async_await2.py 582 B

123456789101112131415161718192021222324252627
  1. # test await expression
  2. import sys
  3. if sys.implementation.name == 'micropython':
  4. # uPy allows normal generators to be awaitables
  5. coroutine = lambda f: f
  6. else:
  7. import types
  8. coroutine = types.coroutine
  9. @coroutine
  10. def wait(value):
  11. print('wait value:', value)
  12. msg = yield 'message from wait(%u)' % value
  13. print('wait got back:', msg)
  14. return 10
  15. async def f():
  16. x = await wait(1)**2
  17. print('x =', x)
  18. coro = f()
  19. print('return from send:', coro.send(None))
  20. try:
  21. coro.send('message from main')
  22. except StopIteration:
  23. print('got StopIteration')