简体   繁体   English

尝试使用泡菜和多处理时出错

[英]Error when trying to use pickle and multiprocessing

I'm trying to use multiprocessing to run my code in Python 3.7, but met a problem.我正在尝试使用多处理在 Python 3.7 中运行我的代码,但遇到了问题。 There is an error when I try to run my code:当我尝试运行我的代码时出现错误:

Can't pickle local object 'mm_prepare_run.<locals>...

I understand it's a problem with pickle , but I didn't find a proper answer how to resolve this issue.我知道这是pickle的问题,但我没有找到正确的答案来解决这个问题。

My simple code is below.我的简单代码如下。 Could you advise how I can solve the problem?你能告诉我如何解决这个问题吗?

import multiprocessing
import copy
from pathlib import Path

proc_mrg = multiprocessing.Manager()
num_cpu = 8   # number of CPU


def prepare_run(config):
    din['config'] = config
    din_temp = copy.deepcopy(din)
    dout_list.append(proc_mrg.dict({}))

    #process = multiprocessing.Process(target=Run_IDEAS_instance_get_trajectory,args=(din_temp, dout_list[-1]))  
    process = multiprocessing.Process(target=Run_IDEAS_instance_get_trajectory(din_temp, dout_list[-1]))
    proc_list.append(process)

    for job in proc_list:
        job.start()

When you create a Process , in prepare_run you are calling Run_IDEAS_instance_get_trajectory instead of just passing it as a reference.创建Process时,在prepare_run调用Run_IDEAS_instance_get_trajectory而不仅仅是将其作为参考传递。 And since that function does not return a result, the target of the Process is None .而且由于该函数不返回结果,因此ProcesstargetNone

Use this instead:改用这个:

process = multiprocessing.Process(
    target=Run_IDEAS_instance_get_trajectory,
    args=(din_temp, dout_list[-1])
)

Functions in Python are first class objects of the callable type. Python 中的函数是可调用类型的第一类对象。 See the "Data model" chapter in the Python language reference.请参阅 Python 语言参考中的“数据模型”一章。

Edit:编辑:

From your comment, I can see that you are running this code on ms-windows.从您的评论中,我可以看到您正在 ms-windows 上运行此代码。 On this platform it is required that you run process creation inside a if __name__ == "__main__" block!在此平台上,您需要if __name__ == "__main__"块内运行进程创建! Because of how multiprocessing works on this platform, python has to be able to import your script without side effects such as starting a new process.由于multiprocessing在这个平台上的工作方式,python 必须能够导入您的脚本而不会产生副作用,例如启动一个新进程。 See the "programming guidelines" section in the documentation for multiprocessing .请参阅multiprocessing文档中的“编程指南”部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM