async enumeration - possible?
Frank Millman
frank at chagford.com
Tue Nov 29 09:25:43 EST 2016
Hi all
Python 3.6 has introduced Asynchronous Generators, which work very well.
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 counter(n):
... for i in range(n):
... yield i
...
>>> async def main():
... c = counter(5)
... async for j in c:
... print(j)
... print('done')
...
>>> loop.run_until_complete(main())
0
1
2
3
4
done
However, it does not allow you to enumerate over the generator output -
>>> async def main():
... c = counter(5)
... async for j, k in enumerate(c):
... print(j, k)
... print('done')
...
>>> 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
>>>
Is there any technical reason for this, or is it just that no-one has got
around to writing an asynchronous version yet?
Frank Millman
More information about the Python-list
mailing list