Queue线程安全队列

 

 其中,put和get函数都一个block参数,默认为ture。表示如果队列中为满或者空,那么就会一直阻塞,直到队列可操作。

 

 1 import threading,time
 2 from queue import Queue
 3 
 4 def set_value(q):
 5     index = 0
 6     while True:
 7         q.put(index)
 8         index += 1
 9         time.sleep(3)
10 
11 def get_value(q):
12     while True:
13         print (q.get())
14 
15 def main():
16     q = Queue(4)
17     t1 = threading.Thread(target=set_value, args=[q])
18     t2 = threading.Thread(target=get_value, args=[q])
19 
20     t1.start()
21     t2.start()
22 
23 if __name__ == '__main__':
24     main()

 

posted @ 2020-06-07 14:53  牛公的跑奔  阅读(477)  评论(0编辑  收藏  举报
总访问量:AmazingCounters.com