问题描述
multiprocessing.Pool
快把我逼疯了...
我想升级许多软件包,并且对于每个软件包,我都必须检查是否有更高版本.这是由 check_one
函数完成的.
主要代码在 Updater.update
方法中:在那里我创建了 Pool 对象并调用 map()
方法.
multiprocessing.Pool
is driving me crazy...
I want to upgrade many packages, and for every one of them I have to check whether there is a greater version or not. This is done by the check_one
function.
The main code is in the Updater.update
method: there I create the Pool object and call the map()
method.
代码如下:
当我运行它时,我得到了这个奇怪的错误:
When I run it I get this weird error:
推荐答案
multiprocessing 通过 将任务(包括
.与 check_one
和 data
)传递给工作进程mp.SimpleQueueQueue.Queue
不同,放在 mp.SimpleQueue
中的所有内容都必须是可选择的.Queue.Queue
s 是不可挑选的:
multiprocessing passes tasks (which include check_one
and data
) to the worker processes through a mp.SimpleQueue
. Unlike Queue.Queue
s, everything put in the mp.SimpleQueue
must be pickable. Queue.Queue
s are not pickable:
产生此异常:
您的 data
包括 packages
,这是一个 Queue.Queue.这可能是问题的根源.
Your data
includes packages
, which is a Queue.Queue. That might be the source of the problem.
这是一个可能的解决方法:Queue
用于两个目的:
Here is a possible workaround: The Queue
is being used for two purposes:
- 找出近似大小(通过调用
qsize
) - 存储结果以供日后检索.
我们可以使用 mp.Value
,而不是调用 qsize
,以便在多个进程之间共享一个值.
Instead of calling qsize
, to share a value between multiple processes, we could use a mp.Value
.
我们可以(并且应该)只返回来自对 check_one
的调用的值,而不是将结果存储在队列中.pool.map
将结果收集到自己制作的队列中,并将结果作为 pool.map
的返回值返回.
Instead of storing results in a queue, we can (and should) just return values from calls to check_one
. The pool.map
collects the results in a queue of its own making, and returns the results as the return value of pool.map
.
例如:
这篇关于multiprocessing.Pool - PicklingError: Can't pickle <type 'thread.lock'>: 属性查找 thread.lock 失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!