Search code examples
pythonsubprocessstdoutpopen

Subprocess.Popen route child process stdout,stderr to parent process


I'd like to call a background task using subprocess.Popen that will run a server on the background, that suppose to print some meaningful messages upon some events like incoming http message.

I want to get those logs and merge them with the parent process so I could observe the whole flow, which include other components besides this server.

According to documentation

PIPE indicates that a new pipe to the child should be created.

So I've tried the follow call, but failed to trace the child process logs.

res = subprocess.Popen("python3 http_server.py", 
                       shell=True,
                       stdin=PIPE, 
                       stdout=PIPE, 
                       stderr=STDOUT, 
                       close_fds=True)

Any idea how to fix it ?


Solution

  • Try removing all the options and only provide the script to the subprocess. It should display all logs for the child process in the same terminal.

    res = subprocess.Popen("python3 http_server.py")