我使用python'multiprocessing'模块在多个内核上运行单个进程,但是我想并行运行几个独立的进程。
例如,进程1解析大型文件,进程2解析不同文件中的模式,进程3进行一些计算;具有不同参数集的所有这三个不同的处理程序是否可以并行运行?
def Process1(largefile):
Parse large file
runtime 2hrs
return parsed_file
def Process2(bigfile)
Find pattern in big file
runtime 2.5 hrs
return pattern
def Process3(integer)
Do astronomical calculation
Run time 2.25 hrs
return calculation_results
def FinalProcess(parsed,pattern,calc_results):
Do analysis
Runtime 10 min
return final_results
def main():
parsed = Process1(largefile)
pattern = Process2(bigfile)
calc_res = Process3(integer)
Final = FinalProcess(parsed,pattern,calc_res)
if __name__ == __main__:
main()
sys.exit()
在上面的伪代码中,Process1,Process2和Process3是单核进程,即它们不能在多个处理器上运行。这些过程按顺序运行,耗时2 + 2.5 + 2.25hrs = 6.75 hrs。是否可以并行运行这三个过程?因此,它们可以同时在不同的处理器/内核上运行,并且在大多数时间(Process2)完成之后,我们才能进入最终流程。 最佳答案
从16.6.1.5. Using a pool of workers:
from multiprocessing import Pool
def f(x):
return x*x
if __name__ == '__main__':
pool = Pool(processes=4) # start 4 worker processes
result = pool.apply_async(f, [10]) # evaluate "f(10)" asynchronously
print result.get(timeout=1) # prints "100" unless your computer is *very* slow
print pool.map(f, range(10)) # prints "[0, 1, 4,..., 81]"
因此,您可以对池应用apply_async,并在一切准备就绪后获取结果。
from multiprocessing import Pool
# all your methods declarations above go here
# (...)
def main():
pool = Pool(processes=3)
parsed = pool.apply_async(Process1, [largefile])
pattern = pool.apply_async(Process2, [bigfile])
calc_res = pool.apply_async(Process3, [integer])
pool.close()
pool.join()
final = FinalProcess(parsed.get(), pattern.get(), calc_res.get())
# your __main__ handler goes here
# (...)