关于以下示例:
import os
NUM_CPUS = None # defaults to all available
def worker(f1, f2):
os.system("run program x on f1 and f2") <--- Big command, cannot run more that K in parallel
def test_run(pool):
filelist = os.listdir(files_dir)
for f1 in filelist:
for f2 in filelist:
pool.apply_async(worker, args=(f1, f2))
if __name__ == "__main__":
import multiprocessing as mp
pool = mp.Pool(NUM_CPUS)
test_run(pool)
pool.close()
pool.join()
每次os.system调用都会消耗大量资源,因此我无法并行调度超过 K( 5) 的资源。不幸的是,即使NUM_POOLS=5每个设置都会pool.apply_async立即返回。如何指定python不让超过5个worker并行运行?
万千封印
相关分类