kopia lustrzana https://github.com/micropython/micropython-lib
asyncio_slow: Add Future examples from docs.
rodzic
30a0586732
commit
0b1d9a0493
|
@ -0,0 +1,15 @@
|
|||
#https://docs.python.org/3.4/library/asyncio-task.html#example-chain-coroutines
|
||||
#import asyncio
|
||||
import asyncio_slow as asyncio
|
||||
|
||||
@asyncio.coroutine
|
||||
def slow_operation(future):
|
||||
yield from asyncio.sleep(1)
|
||||
future.set_result('Future is done!')
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
future = asyncio.Future()
|
||||
asyncio.Task(slow_operation(future))
|
||||
loop.run_until_complete(future)
|
||||
print(future.result())
|
||||
loop.close()
|
|
@ -0,0 +1,21 @@
|
|||
#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()
|
Ładowanie…
Reference in New Issue