texasolz.blogg.se

Python subprocess run command in background
Python subprocess run command in background






I have a python script invoked with Python 2.7.15 spawning another python script using Python 3.8.5, so it matches your scenario. Print("Runner started! Runner output follows.")įor line in iter(p.stdout.readline, b''):Īnd the current time from runner.py will be displayed in the executor.py script:Ĭode: Select all Aaron% python executor.py P = Popen(, stdout=PIPE, bufsize=1) # Note the "-u" flag. My demo below works with the "-u" and it does nothing at all without the flag.Ĭode: Select all Aaron% cat runner.pyĬode: Select all Aaron% cat executor.py It appears that you need to tell the spawned python process to not buffer its output by passing the "-u" flag. Maybe this will point you in the right direction or at least make you aware of the subprocess module if you haven't heard of it. I need to create a background process that will wait for incoming commands and perfom them. Here is a snippet taken from one of the examples where you can write to the stdin of the spawned process (you can do something similar for reading from stdout as well):Ĭode: Select all proc = subprocess.Popen(, stdin=subprocess.PIPE)

python subprocess run command in background

It covers writing to and reading from stdin and stdout of a subprocess: This is the Python 2.7 documentation for the subprocess module: Īnd I think this site has a great writeup with some examples on using the module.

python subprocess run command in background

You can spawn a new process from Python 2.7 code (to run your Python 3 script) and set it up such that the stdin and stdout can be read and written to. I don't have any of my own examples I can provide, but are you familiar with the subprocess module in Python? It sounds like that may be what you want.








Python subprocess run command in background