forked from andaok/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueExam2.py
More file actions
46 lines (33 loc) · 928 Bytes
/
QueueExam2.py
File metadata and controls
46 lines (33 loc) · 928 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
# -*- encoding:utf-8 -*-
'''
Created on 2012-10-28
@author: root
'''
"""
限制队列的大小,如果队列满了,那么控制主线程(threads producer)被阻塞,等待项目被弹出队列.
"""
import threading
import Queue
import time,random
WORKERS = 2
class Worker(threading.Thread):
def __init__(self,queue,thname):
self.__queue = queue
self.__thname = thname
threading.Thread.__init__(self)
def run(self):
while 1:
item = self.__queue.get()
if item is None:
break
#time.sleep(random.randint(10,100)/1000)
time.sleep(2)
print self.__thname,"task",item,"finished"
queue = Queue.Queue(3)
for i in range(WORKERS):
Worker(queue,i+1).start()
for item in range(10):
queue.put(item)
print "push" , item
for i in range(WORKERS):
queue.put(None)