forked from adafruit/circuitpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_await2.py
More file actions
27 lines (22 loc) · 717 Bytes
/
async_await2.py
File metadata and controls
27 lines (22 loc) · 717 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# test await expression
# 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__.
class Awaitable:
def __init__(self, value):
self.value = value
def __await__(self):
print('wait value:', self.value)
msg = yield 'message from wait(%u)' % self.value
print('wait got back:', msg)
return 10
async def f():
x = await Awaitable(1)**2
print('x =', x)
coro = f()
print('return from send:', coro.send(None))
try:
coro.send('message from main')
except StopIteration:
print('got StopIteration')