forked from PacktPublishing/AdvancedPythonProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample3.py
More file actions
47 lines (33 loc) · 936 Bytes
/
example3.py
File metadata and controls
47 lines (33 loc) · 936 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# ch3/example3.py
import threading
from math import sqrt
def is_prime(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
print('%i is a prime number.' % x)
class MyThread(threading.Thread):
def __init__(self, x):
threading.Thread.__init__(self)
self.x = x
def run(self):
print('Starting processing %i...' % x)
is_prime(self.x)
my_input = [2, 193, 323, 1327, 433785907]
threads = []
for x in my_input:
temp_thread = MyThread(x)
temp_thread.start()
threads.append(temp_thread)
for thread in threads:
thread.join()
print('Finished.')