Is this pythonic?

Marko Rauhamaa marko at pacujo.net
Thu Nov 24 06:14:28 EST 2016


Chris Angelico <rosuav at gmail.com>:

> On Thu, Nov 24, 2016 at 9:59 PM, Marko Rauhamaa <marko at pacujo.net> wrote:
>> Chris Angelico <rosuav at gmail.com>:
>>> A coroutine can be abandoned at an await point, but the
>>> currently-executed call is still going to complete (usually);
>>
>> I don't quite understand. Say you are awaiting on receiving bytes from a
>> socket. That means there has been a nonblocking call to read(2),
>> recvmsg(2) or equivalent that has returned EAGAIN. If you now abandon
>> the coroutine, there is no resumption of the system call but the
>> coroutine can finish instantaneously.
>
> Is the read(2) still going to consume data from the pipe/socket?

Only if the kernel has already buffered data it has received previously.

If there isn't yet any data available in the kernel, read(2) returns
with errno=EAGAIN, and the control is returned to the main loop. The
main loop then goes to sleep in epoll_wait(2) or equivalent.

> If so, the operation is still going to continue, whether you use
> coroutines or threads. If not, it would have been cancelled whether
> you use coroutines or threads.

When you use threads, you call read(2) in the blocking mode. Then the
read(2) operation will block "for ever." There's no clean way to cancel
the system call.


Marko



More information about the Python-list mailing list