简体   繁体   English

如何在多处理 function 中传递参数以及如何使用多处理列表?

[英]How to pass argument in multiprocessing function and how to use multiprocessing list?

I am trying to use the multiprocessing in python. I've created a function that appends the value to the list passed to that function ( check_m_process ).我正在尝试在 python 中使用多处理。我创建了一个 function,它将值附加到传递给该 function ( check_m_process ) 的列表中。 I am trying to pass a list ( m ) which is defined outside.我正在尝试传递一个在外部定义的列表( m )。 Since the normal list variable won't update itself outside the multiprocessing function, I've used a multiprocessing list to see changes made my function to the list.由于普通列表变量不会在多处理 function 之外自行更新,因此我使用了多处理列表来查看使我的 function 进入列表的更改。

While executing the function it shows argument error as shown in the below output, instead of passing the argument.在执行 function 时,它显示参数错误,如下面的 output 所示,而不是传递参数。

import multiprocessing

# common list
m =  multiprocessing.Manager().list()

def check_m_process(m):
    print('m before - ',list(m))
    for i in range(5):
        m = m + [i]
    print('m in function - ',list(m))
    
p1 = multiprocessing.Process(target = check_m_process, args=(m))
p1.start()
p1.join()

OUTPUT ERROR: OUTPUT 错误:

Process Process-37:处理过程 37:

Traceback (most recent call last):追溯(最近一次通话):

File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap文件“/usr/lib/python2.7/multiprocessing/process.py”,第 258 行,在 _bootstrap

self.run()自我运行()

File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run运行中的文件“/usr/lib/python2.7/multiprocessing/process.py”,第 114 行

self._target(*self._args, **self._kwargs) self._target(*self._args, **self._kwargs)

TypeError: check_m_process() takes exactly 1 argument (0 given) TypeError: check_m_process() 恰好接受 1 个参数(给定 0)

However, the above function does execute when executed without multiprocessing as check_m_process([]) .但是,上面的 function 在没有多处理的情况下执行时确实会执行check_m_process([]) But when I add some extra parameter, the function does execute as shown in the below output. I am confused about how an argument in multiprocessing function works or how it should actually pass like how to pass just a single argument with multiprocessing function.但是当我添加一些额外的参数时,function 确实执行如下 output 所示。我对多处理 function 中的参数如何工作或者它应该如何实际传递感到困惑,就像如何通过多处理 function 传递单个参数一样。

def check_m_process(tmp_str,m):
    print('m before - ',list(m))
    for i in range(5):
        m = m + [i]
    print('m in function - ',list(m))

p1 = multiprocessing.Process(target = check_m_process, args=('',m))
p1.start()
p1.join()

OUTPUT: OUTPUT:

('m before - ', []) ('之前-',[])

('m in function - ', [0, 1, 2, 3, 4]) (在 function - ', [0, 1, 2, 3, 4])

So after the execution of the function, I was hoping the list defined ( m ) must have updated now after function execution as per the shown above output.因此,在执行 function 之后,我希望定义的列表 ( m ) 在执行 function 之后必须按照上面显示的 output 进行更新。

print('m outside function - ',list(m))

OUTPUT: OUTPUT:

('m outside function - ', []) (在 function 之外 - ',[])

But after printing the value of list m , it shows empty instead of defining the variable as a multiprocessing list in the beginning.但是在打印列表m的值之后,它显示为空,而不是在开始时将变量定义为多处理列表。

Can someone help me with how to pass a single parameter in the multiprocessing function and how to use the common list throughout the multiprocessing function?有人可以帮助我了解如何在多处理 function 中传递单个参数以及如何在整个多处理 function 中使用公共列表吗? Or is there any other way to deal with it?或者有什么其他的方法可以处理吗?

For your first problem, you need to pass (m,) at the argument (note the trailing comma).对于您的第一个问题,您需要在参数处传递(m,) (注意尾随逗号)。 That is the syntax required to create a single-element tuple in Python. When you just surround a single item with parenthesis, no tuple is created.这是在 Python 中创建单元素元组所需的语法。当您仅用括号括起单个项目时,不会创建元组。

For your second problem, you need to just append items to the multiprocessing.Manager().list() , instead of re-assigning the variable repeatedly:对于你的第二个问题,你只需要 append 项目到multiprocessing.Manager().list() ,而不是重复重新分配变量:

for i in range(5):
    m.append(i)

The way you're currently doing it is actually creating a regular Python list, and assigning m to that, not updating your multiprocessing list你目前这样做的方式实际上是创建一个常规的 Python 列表,并将m分配给它,而不是更新你的multiprocessing列表

>>> i = multiprocessing.Manager().list()
>>> i
<ListProxy object, typeid 'list' at 0x7fa18483b590>
>>> i = i + ['a']
>>> i
['a']

Notice how i is no longer a ListProxy after I concatenate it with ['a'] , it's just a regular list .请注意,在我将它与['a']连接后, i不再是ListProxy ,它只是一个常规list Using append avoids this:使用append可以避免这种情况:

>>> i = multiprocessing.Manager().list()
>>> i
<ListProxy object, typeid 'list' at 0x7fa18483b6d0>
>>> i.append('a')
>>> i
<ListProxy object, typeid 'list' at 0x7fa18483b6d0>
>>> str(i)
"['a']"

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

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