简体   繁体   English

Python 多处理:将 kwargs 转发到 actor 函数

[英]Python multiprocessing: forwarding kwargs to actor function

Note: Python Multiprocessing - How to pass kwargs to function?注意: Python 多处理 - 如何将 kwargs 传递给函数? does not answer this question.不回答这个问题。

procs.append(Process(target=fn, args=(cmd, results), kwargs=**kwargs))

Pycharm shows syntax error expression expected at ** for kwargs=**kwargs . Pycharm 显示了在** for kwargs=**kwargs expression expected语法错误expression expected

How should I pass the kwargs in without expanding?我应该如何在不扩展的情况下传递kwargs

I tried to use kwargs=kwargs as following我尝试使用kwargs=kwargs如下

import multiprocessing
from multiprocessing import Process
import time


c = 0
def fn(a, res, **kwargs):
    print('sleeping: {}'.format(kwargs))
    time.sleep(10)
    global c
    c+=1
    res[a]=c
    print(res)

def test(**kwargs):
    cmds = ['1','2','3']
    procs = []
    manager = multiprocessing.Manager()
    results = manager.dict()
    for cmd in cmds:
        procs.append(Process(target=fn, args=(cmd, results), kwargs=kwargs))
        procs[-1].start()
    for proc in procs:
        proc.join()
    print(results)

if __name__ == '__main__':
    test(a=1, b=2)

But got the error:但是得到了错误:

self._target(*self._args, **self._kwargs)
TypeError: fn() got multiple values for argument 'a'
Process Process-3:

You have a variable name a in kwargs therefore the a parameter in function fn is defined in both cmd in kwargs .您在kwargs有一个变量名称a因此函数fna参数在kwargs cmd中都定义了。 Try changing the param a in def fn(a, res, **kwargs): to another name or change a in test(a=1, b=2) to something like test(e=1, b=2)尝试将def fn(a, res, **kwargs):的参数a更改为另一个名称或将test(a=1, b=2)更改a test(e=1, b=2)

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

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