简体   繁体   English

Python(多处理):如何将字典作为工作进程初始值设定项函数的参数传递?

[英]Python (multiprocessing): How to pass a dictionary as the argument of a worker process initializer function?

I'm using a function to initialize the worker processes of a process pool, and this function has a single argument, which is a dictionary.我正在使用一个函数来初始化进程池的工作进程,这个函数有一个参数,它是一个字典。 When the process pool is created and the function is called to initialize each worker process I get an error regarding the wrong number of arguments:创建进程池并调用该函数以初始化每个工作进程时,我收到有关参数数量错误的错误:

TypeError: _init_worker() takes 1 positional argument but 2 were given

The process initializer function being used:正在使用的进程初始化函数:

def _init_worker(shared_arrays):

    _global_shared_arrays = shared_arrays

The initializer is being called in the normal way for each worker process:每个工作进程都以正常方式调用初始化程序:

with multiprocessing.Pool(processes=_NUMBER_OF_WORKER_PROCESSES,
                          initializer=_init_worker, initargs=(arrays_dict)) as pool:

I think this has something to do with how dictionaries are passed as the argument, in that the above error always lists the number of items in the dictionary as the number of positional arguments that were passed, as if what's being passed is the keys of the dictionary rather than the dictionary itself.我认为这与字典作为参数传递的方式有关,因为上面的错误总是将字典中的项目数列为传递的位置参数的数量,就好像传递的是字典而不是字典本身。 When I step into the code in the debugger this is exactly what's going on, ie if there's a single item in the dictionary argument then only the key is passed through to the initializer function, rather than the dictionary itself.当我进入调试器中的代码时,这正是正在发生的事情,即如果字典参数中有一个项目,那么只有键被传递给初始化函数,而不是字典本身。

If there are multiple items in the dictionary used as the argument being passed to the initializer function then the above error message is displayed, reporting the number of items in the dictionary as the number of positional arguments given, so it's somehow passing the keys of the dictionary as arguments rather than the dictionary itself.如果字典中有多个项用作传递给初始化函数的参数,则会显示上述错误消息,将字典中的项数报告为给定的位置参数数,因此它以某种方式传递了字典作为参数而不是字典本身。

What am I doing wrong here?我在这里做错了什么?

if you look at the documentation here如果您查看此处的文档

you will see the following:您将看到以下内容:

If initializer is not None then each worker process will   
call initializer(*initargs) when it starts.  

as you can see the the args for the initializer function are are being unpacked by the * operator.正如您所看到的, initializer函数的参数正在被*运算符解包。
So your custom init function should be ready to accept more than one argument in case you pass it a dict with more than one element or else it will fail.因此,您的自定义 init 函数应该准备好接受多个参数,以防您向它传递一个包含多个元素的字典,否则它将失败。
Something like this: def _init_worker(*shared_arrays)像这样: def _init_worker(*shared_arrays)

initargs 将被解包,所以你必须传递一个元组,比如

initargs=(arrays_dict,)

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

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