forked from andaok/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueExam1.py
More file actions
43 lines (30 loc) · 717 Bytes
/
QueueExam1.py
File metadata and controls
43 lines (30 loc) · 717 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
# -*- encoding:utf-8 -*-
'''
Created on 2012-10-28
@author: root
'''
"""
在多个线程里访问同一个队列对象
"""
import threading
import Queue
import time,random
WORKERS = 2
class Worker(threading.Thread):
def __init__(self,queue):
self.__queue = queue
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)
print "task" , item ,"finished"
queue = Queue.Queue(0)
for i in range(WORKERS):
Worker(queue).start()
for i in range(10):
queue.put(i)
for i in range(WORKERS):
queue.put(None)