多处理命令列表
Multiprocess a list of commands
我正在尝试 运行 命令列表。问题是列表可能很长,因此同时具有多个命令 运行 会很棒。
如何使用多处理模块执行此操作?
list_of_commands = [cmd foo, cmd bar, ...]
main_log_file = open( os.getcwd() + '/Error.log', 'w+')
Count = 0
for Job in list_of_commands:
Count += 1
child = subprocess.Popen(Job, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
streamdata = child.communicate()[0]
errcode = child.returncode
if errcode == 0:
print ( 'Job', Count, 'Success' )
elif errcode == 1:
print ( 'Job', Count, 'Completed With Errors' )
elif errcode == 2:
print ( 'Job', Count, 'Error' )
main_log_file.write ( streamdata.decode('ascii') + str(errcode) + '\n' )
main_log_file.close()
执行顺序无关紧要
您可以使用 concurrent.futures.ThreadPoolExecutor map
函数来 运行 恒定数量的并行执行。
WORKERS = 5 # amount of concurrent executions you want
def launcher(job):
child = subprocess.Popen(job, ... )
streamdata = child.communicate()[0]
...
with concurrent.futures.ThreadPoolExecutor(max_workers=WORKERS) as pool:
pool.map(launcher, jobs)
我正在尝试 运行 命令列表。问题是列表可能很长,因此同时具有多个命令 运行 会很棒。
如何使用多处理模块执行此操作?
list_of_commands = [cmd foo, cmd bar, ...]
main_log_file = open( os.getcwd() + '/Error.log', 'w+')
Count = 0
for Job in list_of_commands:
Count += 1
child = subprocess.Popen(Job, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
streamdata = child.communicate()[0]
errcode = child.returncode
if errcode == 0:
print ( 'Job', Count, 'Success' )
elif errcode == 1:
print ( 'Job', Count, 'Completed With Errors' )
elif errcode == 2:
print ( 'Job', Count, 'Error' )
main_log_file.write ( streamdata.decode('ascii') + str(errcode) + '\n' )
main_log_file.close()
执行顺序无关紧要
您可以使用 concurrent.futures.ThreadPoolExecutor map
函数来 运行 恒定数量的并行执行。
WORKERS = 5 # amount of concurrent executions you want
def launcher(job):
child = subprocess.Popen(job, ... )
streamdata = child.communicate()[0]
...
with concurrent.futures.ThreadPoolExecutor(max_workers=WORKERS) as pool:
pool.map(launcher, jobs)