-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread_example.py
More file actions
45 lines (37 loc) · 1.03 KB
/
thread_example.py
File metadata and controls
45 lines (37 loc) · 1.03 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
import threading
import time
class MyThread(threading.Thread):
def __init__(self, thread_id=None, thread_name=None, thread_type=None):
super().__init__()
self.thread_it = thread_id
self.thread_name = thread_name
self.thread_type = thread_type
def run(self):
if self.thread_type == 1:
self.print_linear()
else:
self.print_squares()
def print_linear(self):
i = 1
while True:
try:
print(i,)
i+=i
time.sleep(2)
except e:
print('Exiting {}'.format(self.thread_name))
def print_squares(self):
i = 1
while True:
try:
print(i,)
i*=i
time.sleep(2)
except e:
print('Exiting {}'.format(self.thread_name))
if __name__=='__main__':
t1 = MyThread(1, 'sequence', 1)
t2 = MyThread(2, 'squares', 2)
t1.start()
t2.start()
print('exiting main')