您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

具有池/队列的Python多个子进程在一个完成时立即恢复输出并在队列中启动下一个作业

具有池/队列的Python多个子进程在一个完成时立即恢复输出并在队列中启动下一个作业

ThreadPool 可能非常适合您的问题,您可以设置工作线程的数量添加作业,然后线程将在所有任务中正常工作。

from multiprocessing.pool import ThreadPool
import subprocess


def 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()
python 2022/1/1 18:33:30 有319人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶