Python subprocess: wait for command to finish before starting next one?

前端 未结 2 440

I\'ve written a Python script that downloads and converts many images, using wget and then ImageMagick via chainedsubprocess calls:

for img in          


        
2条回答
  •  时光取名叫无心
    2021-01-05 00:06

    See Using module 'subprocess' with timeout

    Not sure if this is the proper way of doing it, but this is how I accomplish this:

    import subprocess
    from threading import Thread
    
    def call_subprocess(cmd):
        proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        out, err = proc.communicate()
        if err:
            print err
    
    thread = Thread(target=call_subprocess, args=[cmd])
    thread.start()
    thread.join() # waits for completion.
    

提交回复
热议问题