Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何并行执行具有不同参数的已定义函数_Python_Function_Parallel Processing_Multiprocessing - Fatal编程技术网

Python 如何并行执行具有不同参数的已定义函数

Python 如何并行执行具有不同参数的已定义函数,python,function,parallel-processing,multiprocessing,Python,Function,Parallel Processing,Multiprocessing,我有一个具有多个参数的函数。我需要的是一个代码块,它将使用3组不同的参数并行执行我的函数,然后在运行我的另一个代码块之前一直保持到所有进程都完成为止 我试过了,但结果不是我想要的 import multiprocessing from itertools import product bs = [] def a(i): for x in range(i): print(i) b = x + 1 bs.append(b) retur

我有一个具有多个参数的函数。我需要的是一个代码块,它将使用3组不同的参数并行执行我的函数,然后在运行我的另一个代码块之前一直保持到所有进程都完成为止

我试过了,但结果不是我想要的

import multiprocessing
from itertools import product

bs = []
def a(i):
    for x in range(i):
        print(i)
        b = x + 1
        bs.append(b)
    return bs

if __name__ == '__main__':
    i = range(4)
    with multiprocessing.Pool(processes=3) as pool:
        result = pool.starmap(a, product(i))
    print(result)
结果是:

1
2
2
3
3
3
[[], [1], [1, 1, 2], [1, 1, 2, 1, 2, 3]]

2
2
3
3
3
Process Process-1:
Traceback (most recent call last):
  File "E:\Python38-32\lib\multiprocessing\process.py", line 315, in _bootstrap
    self.run()
  File "E:\Python38-32\lib\multiprocessing\process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
TypeError: 'list' object is not callable
Process Process-2:
Traceback (most recent call last):
  File "E:\Python38-32\lib\multiprocessing\process.py", line 315, in _bootstrap
    self.run()
  File "E:\Python38-32\lib\multiprocessing\process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
TypeError: 'list' object is not callable

我读过
multi-processing
中的线程,其中有
.join()
,但我不太了解如何基于它们编写代码

编辑:

我也尝试了这个,但得到了一个错误:

from multiprocessing import Process

bs = []
def a(i):
    for x in range(i):
        print(i)
        b = x + 1
        bs.append(b)
    return bs

if __name__ == '__main__':
    p1 = Process(target=a(2))
    p1.start()
    p2 = Process(target=a(3))
    p2.start()
    p1.join()
    p2.join()
结果是:

1
2
2
3
3
3
[[], [1], [1, 1, 2], [1, 1, 2, 1, 2, 3]]

2
2
3
3
3
Process Process-1:
Traceback (most recent call last):
  File "E:\Python38-32\lib\multiprocessing\process.py", line 315, in _bootstrap
    self.run()
  File "E:\Python38-32\lib\multiprocessing\process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
TypeError: 'list' object is not callable
Process Process-2:
Traceback (most recent call last):
  File "E:\Python38-32\lib\multiprocessing\process.py", line 315, in _bootstrap
    self.run()
  File "E:\Python38-32\lib\multiprocessing\process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
TypeError: 'list' object is not callable

由于多重处理有点复杂,你能帮我解决这个问题吗?。
谢谢。

结果有什么问题?你想做什么使结果不是这样?@J'e:我要找的结果是这样的
[[1],[1,2],[1,2,3],[1,2,3,4]
注意
args=('bob',)后面的悬空逗号。
不要调用你的函数,让多处理调用它结果有什么问题?你想做什么,结果不是这样的?@J'e:我正在寻找的结果是这样的
[[1],[1,2],[1,2,3],[1,2,3,4]
注意
args=('bob',)后面的悬空逗号。
不要调用你的函数,让多处理调用它