forked from peterhinch/micropython-async
-
Notifications
You must be signed in to change notification settings - Fork 1
/
awaitable.py
35 lines (27 loc) · 808 Bytes
/
awaitable.py
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
28
29
30
31
32
33
34
35
# awaitable.py Demo of an awaitable class
# Author: Peter Hinch
# Copyright Peter Hinch 2017 Released under the MIT license
# runs in CPython and MicroPython
# Trivial fix for MicroPython issue #2678
try:
import asyncio_priority as asyncio
except ImportError:
try:
import uasyncio as asyncio
except ImportError:
import asyncio
class Hardware(object):
def __init__(self, count):
self.count = count
def __await__(self): # Typical use, loop until an interface becomes ready.
while self.count:
print(self.count)
yield
self.count -= 1
__iter__ = __await__ # issue #2678
loop = asyncio.get_event_loop()
hardware = Hardware(10)
async def run():
await hardware
print('Done')
loop.run_until_complete(run())