我发现在多重处理中使用池子模块非常适合在Python脚本中同时运行多个进程。
仔细查看示例#启动多个异步计算
可能使用更多进程
。一旦理解了这些行的功能, 我构建的下一个示例就有意义了。
import numpy as np
from multiprocessing import Pool
def desired_function(option, processes, data, etc...):
# your code will go here. option allows you to make choices within your script
# to execute desired sections of code for each pool or subprocess.
return result_array # "for example"
result_array = np.zeros("some shape") # This is normally populated by 1 loop, lets try 4.
processes = 4
pool = Pool(processes=processes)
args = (processes, data, etc...) # Arguments to be passed into desired function.
multiple_results = []
for i in range(processes): # Executes each pool w/ option (1-4 in this case).
multiple_results.append(pool.apply_async(param_process, (i+1,)+args)) # Syncs each.
results = np.array(res.get() for res in multiple_results) # Retrieves results after
# every pool is finished!
for i in range(processes):
result_array = result_array + results[i] # Combines all datasets!
这段代码基本上是运行给定数量的进程所需的功能。您将需要仔细确保您的函数可以区分每个进程(这就是我添加option变量的原因。)而且, 它不必是最后一个填充的数组, 但是对于我的示例, 这就是我的用法。希望这可以使它更容易或帮助您更好地理解Python中的多处理功能!