使用队列 [英] use of Queue
问题描述
如何使用Queue?我在python中找到了以下代码
手册,但我不知道如何在所有项目
生成后停止消费者。我尝试了不同的方法但是所有这些方法
似乎不正确(种族,死锁或重复队列功能)
def worker():
而True :
item = q.get()
do_work(item)
q.task_done()
q =队列()
我在范围内(num_worker_threads):
t =线程(目标=工人)
t.setDaemon(真的)
t.start()
for source()中的项目:
q.put(item)
q.join()#阻止所有任务完成
how is Queue intended to be used? I found the following code in python
manual, but I don''t understand how to stop consumers after all items
have been produced. I tried different approaches but all of them
seemed incorrect (race, deadlock or duplicating queue functionality)
def worker():
while True:
item = q.get()
do_work(item)
q.task_done()
q = Queue()
for i in range(num_worker_threads):
t = Thread(target=worker)
t.setDaemon(True)
t.start()
for item in source():
q.put(item)
q.join() # block until all tasks are done
推荐答案
Alexandru Mosoi写道:
Alexandru Mosoi wrote:
如何使用Queue?我在python中找到了以下代码
手册,但我不知道如何在所有项目
生成后停止消费者。我尝试了不同的方法但是所有这些方法
似乎不正确(比赛,死锁或重复队列功能)
def worker():
而True:
item = q.get()
do_work(item)
q.task_done()
q =队列()
我在范围内(num_worker_threads):
t =线程(目标=工人)
t.setDaemon(True)
t.start()
for source()中的项目:
q.put(项目)
q.join()#block直到完成所有任务
how is Queue intended to be used? I found the following code in python
manual, but I don''t understand how to stop consumers after all items
have been produced. I tried different approaches but all of them
seemed incorrect (race, deadlock or duplicating queue functionality)
def worker():
while True:
item = q.get()
do_work(item)
q.task_done()
q = Queue()
for i in range(num_worker_threads):
t = Thread(target=worker)
t.setDaemon(True)
t.start()
for item in source():
q.put(item)
q.join() # block until all tasks are done
将一个标记放入队列中解释为终止对于
工人。你当然需要为每个工人把它放在那里。
Diez
Put a sentinel into the queue that gets interpreted as "terminate" for the
workers. You need of course to put it in there once for each worker.
Diez
Alexandru Mosoi写道:
Alexandru Mosoi wrote:
如何使用Queue?我在python中找到了以下代码
手册,但我不知道如何在所有项目
生成后停止消费者。我尝试了不同的方法但是所有这些方法
似乎不正确(比赛,死锁或重复队列功能)
def worker():
而True:
item = q.get()
how is Queue intended to be used? I found the following code in python
manual, but I don''t understand how to stop consumers after all items
have been produced. I tried different approaches but all of them
seemed incorrect (race, deadlock or duplicating queue functionality)
def worker():
while True:
item = q.get()
如果item为None:
break
if item is None:
break
do_work(item)
q.task_done()
q = Queue()
for i in range(num_worker_threads):
t =主题(目标=工人)
t.setDaemon(真)
t.start()
for source()项目:
q.put(item)
do_work(item)
q.task_done()
q = Queue()
for i in range(num_worker_threads):
t = Thread(target=worker)
t.setDaemon(True)
t.start()
for item in source():
q.put(item)
#停止所有消费者
for i in range(num_worker_threads):
q.put(无)
# stop all consumers
for i in range(num_worker_threads):
q.put(None)
>
q.join()#block直到完成所有任务
>
q.join() # block until all tasks are done
这就是我的工作方式。
- 格哈德
This is how I do it.
-- Gerhard
DiezPut哨兵进入队列,被解释为终止
Diezfor工人。你当然需要把它放在那里一次用于
Diezeach工作者。
或者让消费者守护程序线程使得当生产者完成时
所有非守护程序线程退出,消费者也会这样做。
跳过
DiezPut a sentinel into the queue that gets interpreted as "terminate"
Diezfor the workers. You need of course to put it in there once for
Diezeach worker.
Or make the consumers daemon threads so that when the producers are finished
an all non-daemon threads exit, the consumers do as well.
Skip
这篇关于使用队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!