forked from PacktPublishing/AdvancedPythonProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample4.py
More file actions
48 lines (37 loc) · 1.16 KB
/
example4.py
File metadata and controls
48 lines (37 loc) · 1.16 KB
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
# ch9/example1.py
from math import sqrt
import asyncio
from timeit import default_timer as timer
async def is_prime(x):
print('Processing %i...' % x)
if x < 2:
print('%i is not a prime number.' % x)
elif x == 2:
print('%i is a prime number.' % x)
elif x % 2 == 0:
print('%i is not a prime number.' % x)
else:
limit = int(sqrt(x)) + 1
for i in range(3, limit, 2):
if x % i == 0:
print('%i is not a prime number.' % x)
return
elif i % 100000 == 1:
await asyncio.sleep(0)
print('%i is a prime number.' % x)
async def main():
task1 = loop.create_task(is_prime(9637529763296797))
task2 = loop.create_task(is_prime(427920331))
task3 = loop.create_task(is_prime(157))
await asyncio.wait([task1, task2, task3])
if __name__ == '__main__':
try:
start = timer()
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
print('Took %.2f seconds.' % (timer() - start))
except Exception as e:
print('There was a problem:')
print(str(e))
finally:
loop.close()