Skip to content

Commit dd80d18

Browse files
committed
asyncio_slow: Add example of wait() from docs.
1 parent be628ac commit dd80d18

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

asyncio_slow/test_parallel.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-parallel-execution-of-tasks
2+
#import asyncio
3+
import asyncio_slow as asyncio
4+
5+
@asyncio.coroutine
6+
def factorial(name, number):
7+
f = 1
8+
for i in range(2, number+1):
9+
print("Task %s: Compute factorial(%s)..." % (name, i))
10+
yield from asyncio.sleep(1)
11+
f *= i
12+
print("Task %s: factorial(%s) = %s" % (name, number, f))
13+
14+
tasks = [
15+
asyncio.Task(factorial("A", 2)),
16+
asyncio.Task(factorial("B", 3)),
17+
asyncio.Task(factorial("C", 4))]
18+
19+
loop = asyncio.get_event_loop()
20+
loop.run_until_complete(asyncio.wait(tasks))
21+
loop.close()

0 commit comments

Comments
 (0)