- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我有这样一个简单的任务:
def worker(queue):
while True:
try:
_ = queue.get_nowait()
except Queue.Empty:
break
if __name__ == '__main__':
manager = multiprocessing.Manager()
# queue = multiprocessing.Queue()
queue = manager.Queue()
for i in range(5):
queue.put(i)
processes = []
for i in range(2):
proc = multiprocessing.Process(target=worker, args=(queue,))
processes.append(proc)
proc.start()
for proc in processes:
proc.join()
似乎 multiprocessing.Queue 可以完成我需要的所有工作,但另一方面,我看到许多 manager().Queue() 的示例,无法理解我真正需要什么。看起来像 Manager().Queue() 使用某种代理对象,但我不明白这些目的,因为 multiprocessing.Queue() 在没有任何代理对象的情况下做同样的工作。
所以,我的问题是:
1) multiprocessing.Queue 和 multiprocessing.manager().Queue() 返回的对象有什么区别?
2) 我需要使用什么?
最佳答案
虽然我对这个主题的理解有限,但从我所做的事情来看,multiprocessing.Queue() 和 multiprocessing.Manager().Queue() 之间存在一个主要区别:
Note When an object is put on a queue, the object is pickled and a background thread later flushes the pickled data to an underlying pipe. This has some consequences which are a little surprising, but should not cause any practical difficulties – if they really bother you then you can instead use a queue created with a manager. After putting an object on an empty queue there may be an infinitesimal delay before the queue’s empty() method returns False and get_nowait() can return without raising Queue.Empty. If multiple processes are enqueuing objects, it is possible for the objects to be received at the other end out-of-order. However, objects enqueued by the same process will always be in the expected order with respect to each other.
Warning As mentioned above, if a child process has put items on a queue (and it has not used JoinableQueue.cancel_join_thread), then that process will not terminate until all buffered items have been flushed to the pipe. This means that if you try joining that process you may get a deadlock unless you are sure that all items which have been put on the queue have been consumed. Similarly, if the child process is non-daemonic then the parent process may hang on exit when it tries to join all its non-daemonic children. Note that a queue created using a manager does not have this issue.
通过将队列设置为全局变量并在初始化时为所有进程设置它,可以将 multiprocessing.Queue() 与 Pool 一起使用:
queue = multiprocessing.Queue()
def initialize_shared(q):
global queue
queue=q
pool= Pool(nb_process,initializer=initialize_shared, initargs(queue,))
将创建具有正确共享队列的池进程,但我们可以争辩说 multiprocessing.Queue() 对象不是为此用途而创建的。
另一方面,manager.Queue() 可以在池子进程之间共享,方法是将它作为函数的普通参数传递。
在我看来,使用 multiprocessing.Manager().Queue() 在任何情况下都很好,而且麻烦更少。使用经理可能会有一些缺点,但我不知道。
关于Python multiprocessing.Queue 与 multiprocessing.manager().Queue(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43439194/
我试图弄清楚以下模块正在做什么。 import Queue import multiprocessing import threading class BufferedReadQueue(Queue.
如果我使用 Queue.Queue,那么我的 read() 函数不起作用,为什么?但是,如果我使用 multiprocessing.Queue,它运行良好: from multiprocessing
我正在寻找比我在文档中找到的更多关于 Python 队列实现的见解。 根据我的理解,如果我在这方面有误,请原谅我的无知: queue.Queue():通过内存中的基本数组实现,因此不能在多个进程之间共
当我使用多处理模块(Windows 上的 Python 2.7)中的队列代替 Queue.Queue 时,我的程序没有完全关闭。 最终,我想使用 multiprocessing.Process 处理
阅读了大量的 JavaScript 事件循环教程,我看到了不同的术语来标识队列存储消息,当调用堆栈为空时,事件循环准备好获取消息: 队列 消息队列 事件队列 我找不到规范的术语来识别它。 甚至 MDN
我收到错误消息“类型队列不接受参数”。当我将更改队列行替换为 PriorityQueue 时,此错误消失并且编译正常。有什么区别以及如何将其更改为编译队列和常规队列? import java.util
如何将项目返回到 queue.Queue?如果任务失败,这在线程或多处理中很有用,这样任务就不会丢失。 docs for queue.Queue.get()说函数可以“从队列中删除并返回一个项目”,但
如何在多个 queue.Queue 上进行“选择”同时? Golang 有 desired feature及其 channel : select { case i1 = 声明。 线程:queue 模
http://docs.python.org/2/library/queue.html#Queue.Queue.put 这似乎是一个幼稚的问题,但我在文档和谷歌搜索中都没有找到答案,那么这些方法是线程
这可能是个愚蠢的问题,但我对与 .dequeue() 和 $.queue() 一起使用的 .queue() 感到困惑> 或 jquery.queue()。 它们是否相同,如果是,为什么 jquery
我正在尝试创建一个线程化的 tcp 流处理程序类线程和主线程对话,但是 Queue.Queue 也没有做我需要的,服务器从另一个程序接收数据,我只想传递它进入主线程进行处理这里是我到目前为止的代码:
The principal challenge of multi-threaded applications is coordinating threads that share data or ot
在Queue模块的queue类中,有几个方法,分别是qsize、empty 和 full,其文档声称它们“不可靠”。 他们到底有什么不可靠的地方? 我确实注意到 on the Python docs网
我需要一个队列,多个线程可以将内容放入其中,并且多个线程可以从中读取。 Python 至少有两个队列类,Queue.Queue 和 collections.deque,前者似乎在内部使用后者。两者都在
明天我将介绍我选择进程内消息队列实现的基本原理,但我无法阐明我的推理。我的合作设计者提议我们实现一个简单的异步队列,只使用基本的作业列表和互斥锁来控制访问,我建议在嵌入式模式下使用 ActiveMQ。
在 scala 中定义了一个特征: trait Queue[T] Queue 是一种类型吗?或其他东西,例如类型构造函数? 来自 http://artima.com/pins1ed/type-para
我看到 SML/NJ 包含一个队列结构。我不知道如何使用它。如何使用 SML/NJ 提供的附加库? 最佳答案 Queue structure SML '97 未指定,但它存在于 SML/NJ 的顶级环
我是 D3 和 JavaScript 的新手。 我试图理解其中的 queue.js。 我已经完成了 this关联。但是仍然无法清楚地了解 queue.await() 和 queue.awaitAll(
所以我试图在我的 main.cpp 文件中调用一个函数,但我得到“错误:没有匹配函数来调用‘Queue::Queue()。” 队列.h #ifndef QUEUE_H #define QUEUE_H
假设我有一个 10 行的二维 numpy 数组 例如 array([[ 23425. , 521331.40625], [ 23465. , 521246.03125],
我是一名优秀的程序员,十分优秀!