forked from UWPCE-PythonCert/ProgrammingInPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgather.py
More file actions
28 lines (20 loc) · 682 Bytes
/
gather.py
File metadata and controls
28 lines (20 loc) · 682 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/env python
"""
test of gather()
adapted from:
https://docs.python.org/3/library/asyncio-task.html
"""
import asyncio
async def factorial(name, number):
f = 1
for i in range(2, number + 1):
print("Task %s: Compute factorial(%s)..." % (name, i))
await asyncio.sleep(1) # to simulate a longer process...
f *= i
print("Task %s: factorial(%s) = %s" % (name, number, f))
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.gather(factorial("A", 2),
factorial("B", 3),
factorial("C", 4),
))
loop.close()