Python 3 Pool Map 不遵循 Mac 中的进程指令



>我有以下代码

import multiprocessing, subprocess
def runGate(alias):
#worker function
command = ['Gate','-a',alias,'main.mac']
return subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p = multiprocessing.Pool(processes=4)
p.map(runGate, aliases)
p.close()
p.join()

其中别名是一组 1k+ 组合,显然我不想同时启动 1k+ 进程,只有 4。但事实并非如此,实际上启动了 1k+ 进程,我的 mac 重新启动。如何一次只启动 4 个进程?

这是实际有效的解决方案

def call_proc(cmd):
""" This runs in a separate thread. """
#subprocess.call(shlex.split(cmd))  # This will block until cmd finishes
p = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
return (out, err)
pbar = ProgressBar(widgets=[SimpleProgress()], maxval=len(aliases)).start()
pool = multiprocessing.Pool(processes=6)  #(multiprocessing.cpu_count())
results = []
for alias in aliases:
command = 'Gate -a '+alias+' main.mac'
pool.apply_async(call_proc, (command,), callback=results.append)
while len(results) != len(aliases):
pbar.update(len(results))
sleep(0.5)
pbar.finish()
# Close the pool and wait for each running task to complete
pool.close()
pool.join()

相关内容

  • 没有找到相关文章

最新更新