带有多重处理的 python 代码每次只产生一个进程 [英] python code with mulitprocessing only spawns one process each time

查看:35
本文介绍了带有多重处理的 python 代码每次只产生一个进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的python代码,试图将一批jpg文件转换为png.但是这段代码每次在我的linux机器上只生成一个子进程,这段代码有什么问题?

I have a simple python code, trying to convert a batch of jpg file to png. But this code only spawn one subprocess each time on my linux machine, what's the problem with this code?

from scipy.misc import imread, imsave
from multiprocessing import Process, util
import logging

util.log_to_stderr(level=logging.DEBUG)
def imgcvt(inf, outf):
    im=imread(inf)
    imsave(outf,im)

for i in range(3):
    run_list = [];
    threads = [];
    for j in range(1,21):
        fn = str(i*20+j+1)
        run_list.append((fn+".jpg", fn+".png"))
    for dev in run_list:
        proc = Process(target=imgcvt, args=dev)
        proc.start()
        proc.join()

谢谢

推荐答案

在启动每个进程后执行 proc.join() 意味着您在继续之前等待它终止.

Doing a proc.join() after starting every process means you are waiting for it to terminate before continuing.

相反,您应该将所有 Process 对象存储到一个列表中,然后加入所有对象:

Instead, you should store all Process objects to a list, and then join on all of them:

processes = []
for dev in run_list:
    proc = Process(target=imgcvt, args=dev)
    proc.start()
    processes.append(proc)
for proc in processes:
    proc.join()

这篇关于带有多重处理的 python 代码每次只产生一个进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆