async_for.py 609 B

1234567891011121314151617181920212223242526272829
  1. # test basic async for execution
  2. # example taken from PEP0492
  3. class AsyncIteratorWrapper:
  4. def __init__(self, obj):
  5. print('init')
  6. self._it = iter(obj)
  7. async def __aiter__(self):
  8. print('aiter')
  9. return self
  10. async def __anext__(self):
  11. print('anext')
  12. try:
  13. value = next(self._it)
  14. except StopIteration:
  15. raise StopAsyncIteration
  16. return value
  17. async def coro():
  18. async for letter in AsyncIteratorWrapper('abc'):
  19. print(letter)
  20. o = coro()
  21. try:
  22. o.send(None)
  23. except StopIteration:
  24. print('finished')