ThreadPool
可能非常适合您的问题,您可以设置工作线程的数量并添加作业,然后线程将在所有任务中正常工作。
from multiprocessing.pool import ThreadPoolimport subprocessdef work(sample): my_tool_subprocess = subprocess.Popen('mytool {}'.format(sample),shell=True, stdout=subprocess.PIPE) line = True while line: myline = my_tool_subprocess.stdout.readline() #here I parse stdout..num = None # set to the number of workers you want (it defaults to the cpu count of your machine)tp = ThreadPool(num)for sample in all_samples: tp.apply_async(work, (sample,))tp.close()tp.join()