"Frank Millman" wrote in message news:o1k355$da5$1...@blaine.gmane.org...
Hi all
Python 3.6 has introduced Asynchronous Generators, which work very well.
[...]
However, it does not allow you to enumerate over the generator output -
[...]
Is there any technical reason for this, or is it just that no-one has got
around to writing an asynchronous version yet?
Thanks for the replies.
@Ian
Thanks for your explanation and example.
The example is perfect for my simple requirement.
@Terry
I should have googled for aenumerate - didn't think of it.
However, this recipe is based on Python 3.5 and earlier.
Asynchronous Generators, introduced in 3.6, make it much easier.
See PEP 525 for the details -
https://www.python.org/dev/peps/pep-0525/
@Chris
I agree, there is a big difference between functions which consume the
entire iterable before returning the result, and those which behave
asynchronously and return the value on-the-fly.
I happened upon enumerate. You mentioned zip and map, which are also likely
to be useful in the right circumstances.
I did not quite follow your example, as I get the opposite result -
Python 3.6.0b4 (default, Nov 22 2016, 05:30:12) [MSC v.1900 64 bit (AMD64)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
import asyncio
loop = asyncio.get_event_loop()
async def gen(n):
... for i in range(n):
... yield i
...
async def main():
... print([x async for x in gen(5)])
...
loop.run_until_complete(main())
[0, 1, 2, 3, 4]
async def main():
... print(list(x async for x in gen(5)))
...
loop.run_until_complete(main())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File
"C:\Users\User\AppData\Local\Programs\Python\Python36\lib\asyncio\base_events.py",
line 466, in run_until_complete
return future.result()
TypeError: 'async_generator' object is not iterable
Frank
--
https://mail.python.org/mailman/listinfo/python-list