micropython-lib/asyncio_slow/test_future2.py

21 wiersze
509 B
Python

#https://docs.python.org/3.4/library/asyncio-task.html#example-future-with-run-forever
#import asyncio
import asyncio_slow as asyncio
@asyncio.coroutine
def slow_operation(future):
yield from asyncio.sleep(1)
future.set_result('Future is done!')
def got_result(future):
print(future.result())
loop.stop()
loop = asyncio.get_event_loop()
future = asyncio.Future()
asyncio.Task(slow_operation(future))
future.add_done_callback(got_result)
try:
loop.run_forever()
finally:
loop.close()