-
Notifications
You must be signed in to change notification settings - Fork 0
/
multithreading.py
190 lines (172 loc) · 4.3 KB
/
multithreading.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
'''
import time
def cal_cube(no):
time.sleep(2)
for i in no:
print(i*i*i)
def cal_sq(number):
time.sleep(2)
for i in number:
print(i*i)
t=time.time();
arr=[1,2,3]
cal_cube(arr)
cal_sq(arr)
print("Time:", time.time()-t)
'''
'''
import threading
import time
class Hello(threading.Thread):
def run(self):
for i in range(5):
print("Hello")
sleep(1)
class Hi(threading.Thread):
def run(self):
for i in range(5):
print("Hi")
sleep(1)
t1=Hello()
t2=Hi()
t1.start()
sleep(0.2)
t2.start()
t1.join()
t2.join()
print("bye")
'''
'''
import threading
import time
class myThread(threading.Thread):
def __init__(self,threadID, name, counter):
threading.Thread.__init__(self)
self.threadID=threadID;
self.name=name;
self.counter=counter
def run(self):
print("starting: "+self.name)
print_time(self.name,5,self.counter)
print("Exiting "+self.name)
def print_time(threadName, counter, delay):
while counter:
time.sleep(delay)
print("%s,%s"%(threadName,time.ctime(time.time())))
counter-=1
thread1 = myThread(1, "Thread-1", 1)
thread2 = myThread(2, "Thread-2", 2)
thread1.start()
thread1.join()
thread2.start()
thread2.join()
'''
'''
import threading
import time
class mythread(threading.Thread):
def __init__(self,threadID,name,counter):
threading.Thread.__init__(self)
self.threadID=threadID
self.name=name
self.counter=counter
def run(self):
print("start it: ",self.name)
threadLock.acquire()
print_time(self.name,self.counter,3)
threadLock.release()
def print_time(threadname,delay,counter):
while counter:
time.sleep(delay)
print("%s,%s"%(threadname,time.ctime()))
counter-=1
threadLock=threading.Lock()
#threads=[]
thread1=mythread(1,"thread-1",0.2)
thread2=mythread(2,"thread-2",0.2)
thread1.start()
thread2.start()
#threads.append(thread1)
#threads.append(thread2)
#for t in threads
# t.join()
thread1,thread2.join()
print("Exiting Main Thread")
'''
'''
import threading
import time
def print_time(threadname,delay,counter):
while counter:
time.sleep(delay)
print("%s,%s"%(threadname,time.ctime()))
counter-=1
class mythread(threading.Thread):
def __init__(self,threadID,name,counter):
threading.Thread.__init__(self)
self.threadID=threadID;
self.name=name;
self.counter=counter;
def run(self):
print("starting: "+self.name)
threadLock.acquire();
print_time(self.name,self.counter,4)
threadLock.release();
threadLock=threading.Lock()
thread1=mythread(1,"thread-1",0.2)
thread2=mythread(2,"thread-2",0.2)
thread1.start()
thread2.start()
thread1,thread2.join()
print("exiting")
'''
import queue
import threading
import time
exitFlag = 0
class myThread (threading.Thread):
def __init__(self, threadID, name, q):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.q = q
def run(self):
print ("Starting " + self.name)
process_data(self.name, self.q)
print ("Exiting " + self.name)
def process_data(threadName, q):
while not exitFlag:
queueLock.acquire()
if not workQueue.empty():
data = q.get()
queueLock.release()
print ("%s processing %s" % (threadName, data))
else:
queueLock.release()
time.sleep(1)
threadList = ["Thread-1", "Thread-2", "Thread-3"]
nameList = ["One", "Two", "Three", "Four", "Five"]
queueLock = threading.Lock()
workQueue = queue.Queue(10)
threads = []
threadID = 1
# Create new threads
for tName in threadList:
thread = myThread(threadID, tName, workQueue)
thread.start()
threads.append(thread)
threadID += 1
# Fill the queue
queueLock.acquire()
for word in nameList:
workQueue.put(word)
queueLock.release()
# Wait for queue to empty
while not workQueue.empty():
pass
# Notify threads it's time to exit
exitFlag = 1
# Wait for all threads to complete
for t in threads:
t.join()
print( "Exiting Main Thread")