python 线程池 阻塞问题
例如:
with ThreadPoolExecutor(max_workers=2, ) as executor: while 1:
#这里会一直pop出来,我想设置了max_workers=2,就pop2个出来,线程执行完了,再进行pop,怎么写?做个计数吗?
data = redis.blpop('key')
future = executor.submit(blpop_task, data)
回答:
如果一直取数据的话,,为什么要让线程执行结束,然后再启动新的线程去做同样的事情,,,,
python">""""""from queue import Queue
from threading import Thread
data_queue = Queue(maxsize=2)
def task():
while True:
data = data_queue.get()
print("task exec finished: %s" % data)
if __name__ == '__main__':
# 启动任务
t1 = Thread(target=task, daemon=False)
t1.start()
t2 = Thread(target=task, daemon=False)
t2.start()
i = 1
while True:
data_queue.put(i)
i += 1
以上是 python 线程池 阻塞问题 的全部内容, 来源链接: utcz.com/a/164436.html