2016-04-13 10:27:06 -04:00
|
|
|
# test await expression
|
|
|
|
|
2020-10-11 02:39:32 -04:00
|
|
|
# uPy allows normal generators to be awaitables.
|
|
|
|
# CircuitPython does not.
|
|
|
|
# In CircuitPython you need to have an __await__ method on an awaitable like in CPython;
|
|
|
|
# and like in CPython, generators do not have __await__.
|
2016-04-13 10:27:06 -04:00
|
|
|
|
2020-10-11 02:39:32 -04:00
|
|
|
class Awaitable:
|
|
|
|
def __init__(self, value):
|
|
|
|
self.value = value
|
|
|
|
|
|
|
|
def __await__(self):
|
|
|
|
print('wait value:', self.value)
|
2021-04-23 15:26:42 -04:00
|
|
|
msg = yield 'message from wait({})'.format(self.value)
|
2020-10-11 02:39:32 -04:00
|
|
|
print('wait got back:', msg)
|
|
|
|
return 10
|
2016-04-13 10:27:06 -04:00
|
|
|
|
|
|
|
async def f():
|
2020-10-11 02:39:32 -04:00
|
|
|
x = await Awaitable(1)**2
|
2016-04-13 10:27:06 -04:00
|
|
|
print('x =', x)
|
|
|
|
|
|
|
|
coro = f()
|
|
|
|
print('return from send:', coro.send(None))
|
|
|
|
try:
|
|
|
|
coro.send('message from main')
|
|
|
|
except StopIteration:
|
|
|
|
print('got StopIteration')
|