Search code examples
pythonprintingsubprocesspopen

How to get prints when using subprocess.Popen


I have been working with using scripts such as

subprocess.Popen(f"py test.py hello &", shell=True, universal_newlines=True)

which will open the script test.py:

import time
import sys

while True:
   print(sys.argv[1])
   time.sleep(1)

However if I run multiply times etc

subprocess.Popen(f"py test.py hello &", shell=True, universal_newlines=True)
subprocess.Popen(f"py test.py world &", shell=True, universal_newlines=True)
subprocess.Popen(f"py test.py stackoverflow &", shell=True, universal_newlines=True)

that means that each of these runs will have its own sys.argv[1] value and will print out over and over again.

However my question is, I want to create a script where I call etc py outprints.py test hello "All test.py with the argv of "hello" should be printed and continue to print during the process of subprocess.Popen(f"py test.py hello &", shell=True, universal_newlines=True)

I wonder if it is possible to do such a script which reads the outlogs of a script that is on in the background and see its outprint and if it is, what can I be able to do that?


Solution

  • Here is an example of reading output from one of your subprocesses back into the main process.

    import subprocess
    
    proc = subprocess.Popen(["python", "test.py", "hello"],
                            stdout=subprocess.PIPE)
    
    # read 5 lines of output from the subprocess and then kill it
    for i in range(5):
        line = proc.stdout.readline()
        print("The subprocess said ", line.decode())
    
    proc.kill()
    

    In your test.py, you should insert a statement to flush the output:

    import time
    import sys
    
    while True:
       print(sys.argv[1])
       sys.stdout.flush()
       time.sleep(1)
    

    This will ensure that the output data is available for reading immediately. Otherwise you will be waiting a long time until you see any output because it will be held in a buffer.