python通过queue模块来提供线程间的通信机制,从而可以让线程分项数据。
个人感觉queue就是管程的概念
一个生产者消费者问题
from random import randint
from threading import Thread
from queue import Queue
from time import sleep def writeq(queue):
print('starting put queue...')
queue.put('hahaha', 1) #1表示在有可用空间前,阻塞
print('size now', queue.qsize()) def readq(queue):
print('starting get queue...')
val = queue.get(1) #1表示在有可用元素前阻塞
print('consume from queu...size now', queue.qsize()) def writer(queue, loops):
for i in range(loops):
writeq(queue)
sleep(randint(1, 3)) def reader(queue, loops):
for i in range(loops):
readq(queue)
sleep(randint(2, 5)) funcs = [writer, reader] def main():
nloops = randint(2, 5)
q = Queue(32) threads = []
for i in range(len(funcs)):
t = Thread(target=funcs[i], args=(q, nloops))
threads.append(t) for i in range(len(funcs)):
threads[i].start() for i in range(len(funcs)):
threads[i].join() print('all done') if __name__ == '__main__':
main()
输出结果: