Python多处理队列 [英] Python Multiprocessing queue

查看:73
本文介绍了Python多处理队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用一组要并行运行的作业填充队列,并使用python的多处理模块来执行此操作.下面的代码段:

I am populating a queue with a set of jobs that I want to run in parallel and using python's multiprocessing module for doing that. Code snippet below:

import multiprocessing
from multiprocessing import Queue
queue = Queue()
jobs = [['a', 'b'], ['c', 'd']]
for job in jobs:
    queue.put(job)

当我执行queue.get()时,我得到以下信息:

When I do queue.get() I get the following:

['a', 'b']

为什么队列中没有填充所有作业?

Why is the queue not getting populated with all the jobs?

推荐答案

实际上是在填充队列.每次将对象放入队列时,都需要调用queue.get().因此,您只需要再调用一次queue.get().

The queue is actually geting populated. You need to call queue.get() for each time you put an object to the queue. So you just need to call queue.get() one more time.

>>> import multiprocessing
>>> from multiprocessing import Queue
>>> queue = Queue()
>>> jobs = [['a', 'b'], ['c', 'd']]
>>> for job in jobs:
    queue.put(job)


>>> queue.get()
['a', 'b']
>>> queue.get()
['c', 'd']

这篇关于Python多处理队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆