如何从python主脚本并行运行多个bash脚本?

时间:2016-09-22 14:25:26

标签: python bash parallel-processing python-multiprocessing

我有一系列耗时的独立bash脚本,可以在python主脚本的7个CPU内核上并行运行。我试图使用multiprocessing.Pool.map()函数实现这一点,迭代数字来自xrange(1,300)序列,其中每个数字用于定义包含要执行的bash脚本的目录的名称。问题是以下脚本为bash脚本运行生成7个进程,并在完成后立即完成。

import multiprocessing
import os
a= os.getcwd() #gets current path
def run(x):
            b = a + '/' + 'dir%r' % (x) # appends the name of targeted folder to path
            os.chdir(b) #switches to the targeted directory
            os.system('chmod +x run.sh')
            os.system('./run.sh') # runs the time consuming script
if __name__ == "__main__":
    procs = 7
    p = multiprocessing.Pool(procs)
    p.map(run, xrange(1, 300))
    print "====DONE===="

我希望其他292 shell脚本也可以运行,那么什么修复或替代实现可以帮助我? 谢谢!

0 个答案:

没有答案