File tree Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change 1+ #https://docs.python.org/3.4/library/asyncio-task.html#example-chain-coroutines
2+ #import asyncio
3+ import asyncio_slow as asyncio
4+
5+ @asyncio .coroutine
6+ def slow_operation (future ):
7+ yield from asyncio .sleep (1 )
8+ future .set_result ('Future is done!' )
9+
10+ loop = asyncio .get_event_loop ()
11+ future = asyncio .Future ()
12+ asyncio .Task (slow_operation (future ))
13+ loop .run_until_complete (future )
14+ print (future .result ())
15+ loop .close ()
Original file line number Diff line number Diff line change 1+ #https://docs.python.org/3.4/library/asyncio-task.html#example-future-with-run-forever
2+ #import asyncio
3+ import asyncio_slow as asyncio
4+
5+ @asyncio .coroutine
6+ def slow_operation (future ):
7+ yield from asyncio .sleep (1 )
8+ future .set_result ('Future is done!' )
9+
10+ def got_result (future ):
11+ print (future .result ())
12+ loop .stop ()
13+
14+ loop = asyncio .get_event_loop ()
15+ future = asyncio .Future ()
16+ asyncio .Task (slow_operation (future ))
17+ future .add_done_callback (got_result )
18+ try :
19+ loop .run_forever ()
20+ finally :
21+ loop .close ()
You can’t perform that action at this time.
0 commit comments