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
Make sure all applications your are calling have valid system return codes when they finish
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.
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 :)
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.