subprocess.wait() not waiting for Popen process to finish (when using threads)?

后端 未结 4 1075
悲&欢浪女
悲&欢浪女 2020-12-03 14:12

I am experiencing some problems when using subprocess.Popen() to spawn several instances of the same application from my python script using threads to have the

相关标签:
4条回答
  • 2020-12-03 14:21

    Make sure all applications your are calling have valid system return codes when they finish

    0 讨论(0)
  • 2020-12-03 14:35

    I was having issues as well, but was inspired by yours.

    Mine looks like this, and works beautifully:

        startupinfo = subprocess.STARTUPINFO()
        startupinfo.dwFlags = subprocess.STARTF_USESHOWWINDOW
        startupinfo.wShowWindow = subprocess.SW_HIDE
        proc = subprocess.Popen(command, startupinfo=startupinfo)
        proc.communicate()
        proc.wait()
    

    Notice that this one hides the window as well.

    0 讨论(0)
  • 2020-12-03 14:37

    Sadly when running your subprocess using shell=True, wait() will only wait for the sh subprocess to finish and not for the command cmd.

    I will suggest if it possible to don't use the shell=True, if not possible you can create a process group like in this answer and use os.waitpid to wait for the process group not just the shell process.

    Hope this was helpful :)

    0 讨论(0)
  • 2020-12-03 14:39

    You could also use check_call() instead of Popen. check_call() waits for the command to finish, even when shell=True and then returns the exit code of the job.

    0 讨论(0)
提交回复
热议问题