-
Notifications
You must be signed in to change notification settings - Fork 0
/
threads1.py
40 lines (29 loc) · 809 Bytes
/
threads1.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
#/bin/python
# Program for messing w/ threads
import threading
import Queue
import time
class WorkerThread(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
def run(self):
print "In WorkerThread"
while True:
counter = self.queue.get()
print "Ordered to sleep for %d seconds!"%counter
time.sleep(counter)
print "Finished sleeping for %d seconds"%counter
self.queue.task_done()
if __name__ == '__main__':
queue = Queue.Queue()
for i in range(10):
print "Creating WorkerThread : %d"%i
worker = WorkerThread(queue)
worker.setDaemon(True)
worker.start()
print "WorkerThread %d Created!"%i
for j in range(10):
queue.put(j)
queue.join()
print "All tasks complete!"