Skip to content

Commit 96cbb50

Browse files
committed
asyncio_slow: Add Future examples from docs.
1 parent 8711a45 commit 96cbb50

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

asyncio_slow/test_future.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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()

asyncio_slow/test_future2.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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()

0 commit comments

Comments
 (0)