如何在Python中使用多处理并传递多个参数,同时还知道正在发生的进程数

  • 本文关键字:进程 Python 处理 参数 python multiprocessing
  • 更新时间 :
  • 英文 :


我需要得到当前正在发生的进程的数量。我知道我能做到以下几点。

import multiprocessing
from multiprocessing import Pool
def f(args):
print("process number "+str(args))

SLICES = 8
with Pool(SLICES) as p:
p.map(f, range(SLICES))

哪个收益率:

process number 0
process number 2
process number 1
process number 3
process number 7
process number 5
process number 4
process number 6

我推测,调用时传递的参数args将给出正在发生的进程数。我知道,为了将多个参数传递给多进程,你必须将其以数组、对象或元组等形式放置。但当我以数组的形式传递多个参数时,我突然无法再访问正在发生的多进程的数量。例如,

def f(args):
return(args[0]+args[1])

SLICES = 8
with Pool(SLICES) as p:
matrices = np.random.random ([16,2]) * 10
matrices = matrices.astype(int)
print(matrices)

result = p.map(f,matrices)
print(result)

结果:

[[5 4]
[9 5]
[8 5]
[8 9]
[8 9]
[7 4]
[2 4]
[7 4]
[7 6]
[3 9]
[4 0]
[9 3]
[7 1]
[5 5]
[6 8]
[8 0]]
[9, 14, 13, 17, 17, 11, 6, 11, 13, 12, 4, 12, 8, 10, 14, 8]

现在,我无法再访问正在发生的进程的数量。我担心的是,我仍然希望传递多个参数,但仍然能够访问数字进程。我该怎么做?

我可能误解了这个问题,因为我不完全确定我知道你说的"过程编号";。由于在您的第一个示例中,它只是您创建并传递给函数的一个id,因此您仍然可以这样做,只需将其作为传入参数的一部分即可。

import multiprocessing
from multiprocessing import Pool
import numpy as np
def f(args):
# index is the "id" or number you pass in that numbers the process.
# arr is the np.random.random matrix that is passed in.
index, arr = args
# Do things with index.
print(f"hello from {index}") 
# Or pass it back...or don't.
return (index, (arr[0] + arr[1]))
# return arr[0] + arr[1]

SLICES = 8
with Pool(SLICES) as p:
matrices = np.random.random([16, 2]) * 10
matrices = matrices.astype(int)
print(matrices)
result = p.map(f, ((idx, val) for idx, val in enumerate(matrices)))
print(result)

结果:

[[5 1]
[5 8]
[5 4]
[2 2]
[9 5]
[7 7]
[8 4]
[6 8]
[0 5]
[5 6]
[8 6]
[4 2]
[0 7]
[3 0]
[5 3]
[6 7]]
[(0, 6), (1, 13), (2, 9), (3, 4), (4, 14), (5, 14), (6, 12), (7, 14), (8, 5), (9, 11), (10, 14), (11, 6), (12, 7), (13, 3), (14, 8), (15, 13)]

相关内容

  • 没有找到相关文章

最新更新