在python中并行运行多个文件的相同功能 [英] Running same function for multiple files in parallel in python
本文介绍了在python中并行运行多个文件的相同功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试为多个文件并行运行一个函数,并希望所有文件都在一个点之前终止.
I am trying to run a function in parallel for multiple files and want all of them to terminate before a point.
例如:有一个循环
def main():
for item in list:
function_x(item)
function_y(list)
现在我想要的是此 function_x
应该对所有项目并行运行.但是应该在调用我的 function_y
之前对所有项目执行此函数.
Now what I want is that this function_x
should run in parallel for all items. But this function should be executed for all items before my function_y
is called.
我打算为此使用芹菜.却不知道该怎么做.
推荐答案
这是我的最终测试代码.
Here is my final test code.
我需要做的就是使用多处理库.
All I needed to do is use multiprocessing library.
from multiprocessing import Process
from time import sleep
Pros = []
def function_x(i):
for j in range(0,5):
sleep(3)
print i
def function_y():
print "done"
def main():
for i in range(0,3):
print "Thread Started"
p = Process(target=function_x, args=(i,))
Pros.append(p)
p.start()
# block until all the threads finish (i.e. block until all function_x calls finish)
for t in Pros:
t.join()
function_y()
这篇关于在python中并行运行多个文件的相同功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文