为什么在 Python 多处理中将启动方法从 'fork' 更改为 'spawn' 不再允许我 运行 我的工作?

Why changing start method to 'spawn' from 'fork' in Python multiprocessing does not allow me run my job anymore?

我能够 运行 使用 multiprocessing.Process 和启动方法 fork 的后台函数。出于某种原因,我需要这个子进程在 运行ning 时启动一个新环境。所以我通过 multiprocessing.set_start_method('spawn') 将启动方法设置为 spawn 并通过 job.start() 将作业设置为 运行 我收到以下错误:

Can't pickle <class 'module'>: attribute lookup module on builtins failed

但是,我在调用的函数中没有对任何东西使用 pickle。我做错了什么?当 运行ning 在 spawn 模式下处理时,是否有我应该遵循的一般经验法则?

仅供参考:我在 Ubuntu 16.04

的机器上

Is there a general rule of thumb...

是的。您 运行 进入此记录的限制:

https://docs.python.org/3/library/multiprocessing.html

There are a few extra restriction which don’t apply to the fork start method.

More picklability

Ensure that all arguments to Process.init() are picklable. Also, if you subclass Process then make sure that instances will be picklable when the Process.start method is called.

您在 ubuntu 上 运行,因此 fork 可能是正确答案。如果您需要解决 fork 不兼容的需求,那么您需要清楚地记录详细信息,作为选择改进解决方案的第一部分。