The Incredible Growth of Python (stackoverflow.blog)

Marko Rauhamaa marko at pacujo.net
Sun Sep 10 08:33:50 EDT 2017


Stephan Houben <stephanh42 at gmail.com.invalid>:

> Op 2017-09-10, Chris Angelico schreef <rosuav at gmail.com>:
>> Want to make something iterable? Define __iter__. Want to make it
>> async-iterable (with "async for")? Define __aiter__. It's a bit clunky
>> if you want the same object to be iterable both ways, but I don't know
>> of any real-world situations where that's the case.
>
> Would we not eventually want a file object to deliver its lines
> asynchronously (with non-blocking reads under the hood) if
> iterated over with "async for", while preserving the current
> blocking behavior in the "for" case?

I'm not exactly sure what your point is.

"async for" is needed to allow a for loop to iterate over a sequence
that is generated asynchronously. If you accidentally use a regular
"for" (and chances are you will), you will experience weird behavior.

As for file objects supporting asynchronous iterators, I agree they
should. Linux is not quite ready for nonblocking file access yet (the
kernel developers are busy trying to make it happen).

Note that you will not only need an async version of a file iterator but
also versions for the "open()" function, directory walking etc.


Marko



More information about the Python-list mailing list