同时运行多个python脚本实例

时间:2013-10-03 10:10:45

标签: python concurrency python-multithreading fork

我正在尝试创建86个task.py实例来同时运行。

import sys
import subprocess

for file in range(86):
    subprocess.call([sys.executable,'task.py',str(file)+'in.csv',str(filen)+'out.csv'])

1 个答案:

答案 0 :(得分:14)

subprocess.call等待命令完成。请改用subprocess.Popen

import sys
import subprocess

procs = []
for i in range(86):
    proc = subprocess.Popen([sys.executable, 'task.py', '{}in.csv'.format(i), '{}out.csv'.format(i)])
    procs.append(proc)

for proc in procs:
    proc.wait()