-
Notifications
You must be signed in to change notification settings - Fork 0
/
py_thread_queue.py
42 lines (39 loc) · 1.37 KB
/
py_thread_queue.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
import threading
import queue
import time
class worker(threading.Thread):
def __init__(self,queue):
threading.Thread.__init__(self)
self.queue=queue
self.thread_stop=False
def run(self):
while not self.thread_stop:
print("thread%d %s: waiting for task" %(self.ident,self.name))
try:
task=q.get(block=True, timeout=20)#接收消息
except queue.Empty:
print("Nothing to do!i will go home!")
self.thread_stop=True
break
print("task recv:%s ,task No:%d" % (task[0],task[1]))
print("i am working")
time.sleep(3)
print("work finished!")
q.task_done()#完成一个任务
res=q.qsize()#判断消息队列大小
if res>0:
print("fuck!There are still %d tasks to do" % (res))
def stop(self):
self.thread_stop = True
if __name__ == "__main__":
q=queue.Queue() #queue的大小
worker=worker(q)
worker.start()
q.put(["produce one cup!",1], block=True, timeout=None)#产生任务消息
q.put(["produce one desk!",2], block=True, timeout=None)
q.put(["produce one apple!",3], block=True, timeout=None)
q.put(["produce one banana!",4], block=True, timeout=None)
q.put(["produce one bag!",5], block=True, timeout=None)
print("***************leader:wait for finish!")
q.join()#等待所有任务完成
print("***************leader:all task finished!")