-
Notifications
You must be signed in to change notification settings - Fork 0
/
asynchronous.py
51 lines (40 loc) · 1.05 KB
/
asynchronous.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""asynchronous in python
"""
import asyncio
async def factorial(number: int) -> int:
"""calculate factorial of number
Args:
number (int): number will calculate it factorial
Returns:
int: factorial of number
"""
result = 1
print(f"started calculate {number}")
for i in range(1, number+1):
result *= i
if i % 10_000 == 0:
await asyncio.sleep(0.1)
print(f"finished calculate {number}")
return result
async def main() -> list:
"""main of codes
Returns:
list: results
"""
numbers = [100000, 20, 300, 1230]
tasks = []
for num in numbers:
tasks.append(loop.create_task(factorial(num)))
await asyncio.wait(tasks)
return tasks
if __name__ == "__main__":
try:
loop = asyncio.get_event_loop()
results = loop.run_until_complete(main())
print("result:")
results = list(map(lambda x: x.result(), results))
print(results[1])
except Exception as err:
print(err)
finally:
loop.close()