I'm working with Python 2.7 on Windows 8/XP.
I have a program A that runs another program B using the following code:
p = Popen(["B"], stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate()
return
B runs a batch script C. C is a long running script and I want B to exit even though C has not finished. I have done it using the following code (in B):
p = Popen(["C"])
return
When I run B, it works as expected. When I run A however, I expected it to exit when B exits. But A waits until C exits even though B has already exitted. Any ideas on what's happening and what possible solutions could be?
Unfortunately, the obvious solution of changing A to look like B is not an option.
Here is a functional sample code to illustrate this issue: https://www.dropbox.com/s/cbplwjpmydogvu2/popen.zip?dl=1
The zip file consists of the following files with the following contents:
A.py
from subprocess import PIPE, Popen
import sys
def log(line):
with open("log.txt", "a") as logfile:
logfile.write(line)
log("\r\n\r\nA: I'll wait for B\r\n")
p = Popen(["C:\\Python27\\python.exe", "B.py"], stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate()
log("A: Done.\r\n")
sys.exit(0)
B.py
from subprocess import Popen, PIPE
import sys
def log(line):
with open("log.txt", "a") as logfile:
logfile.write(line)
log("B: launching C\r\n")
p = Popen(["C.bat"])
log("B: Not waiting for C at all. bye!\r\n")
sys.exit(0)
C.bat
@echo off
echo C: Start long running task : %time% >> "log.txt"
ping -n 10 127.0.0.1>nul
echo C: Stop long running task : %time% >> "log.txt"
Any input is much appreciated.
Popen
is from program B? – Guayaquilclose_fds=True
to the secondPopen()
call help? I'm guessing that C inherits the stdout/stderr pipes from A and thus A waits until C closes them. – Boot