-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathasyncio_demo_2.py
59 lines (44 loc) · 1.5 KB
/
asyncio_demo_2.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
52
53
54
55
56
57
58
59
#!/usr/bin/env python3
"""Asyncio demo finding next prime numbers for a list."""
import asyncio
from math import sqrt
from functools import reduce
def factors(n):
"""Find all factors of a number n.
https://stackoverflow.com/questions/6800193/what-is-the-most-efficient-way-of-finding-all-the-factors-of-a-number-in-python
"""
n_factors = ([i, n//i] for i in range(1, int(sqrt(n) + 1)) if n % i == 0)
return set(reduce(list.__add__, n_factors))
def prime(n):
"""Return True if x is prime, False otherwise."""
if n == 2:
return True
if n % 2 == 0:
return False
if len(factors(n)) == 2:
return True
else:
return False
async def find_next_prime(n):
while True:
n = n + 1
if prime(n):
return n
else:
await asyncio.sleep(0.000000000001)
async def main():
next_prime_1 = loop.create_task(find_next_prime(2123123123112))
next_prime_2 = loop.create_task(find_next_prime(222))
next_prime_3 = loop.create_task(find_next_prime(45022222222))
next_prime_4 = loop.create_task(find_next_prime(1000))
await asyncio.wait([next_prime_1, next_prime_2, next_prime_3, next_prime_4, ])
return next_prime_1, next_prime_2, next_prime_3, next_prime_4
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.set_debug(1)
p1, p2, p3, p4 = loop.run_until_complete(main())
print(p1.result())
print(p2.result())
print(p3.result())
print(p4.result())
loop.close()