- #PYTHON SUBPROCESS GET OUTPUT IN REAL TIME FULL#
- #PYTHON SUBPROCESS GET OUTPUT IN REAL TIME CODE#
- #PYTHON SUBPROCESS GET OUTPUT IN REAL TIME WINDOWS#
I tried this, and for some reason while the code for line in p.stdout:īuffers aggressively, the variant while True: Have you tried omitting the sydout=PIPE so the subprocess writes directly to your console, bypassing the parent process? This loop will stop as soon as the process completes leaving out a need for a break statement or possible infinite loop. ,I used this solution to get realtime output on a subprocess.
Sorry I no matter how many time I look into it, subprocess etcetera is something I just can't ever get to work. Is this supposed to hang indefinitely? I would wish a given solution to also include boilerplate code for editing the loop when the initial subprocess is done. process = await create_subprocess_shell(*command, stdout=PIPE, stderr=PIPE, shell=True) instead of process = await create_subprocess_exec(.).
#PYTHON SUBPROCESS GET OUTPUT IN REAL TIME FULL#
Update: The full loop code is for line in iter(lambda: p.stdout.read(1), ''):Īlso, you pass your command as a string.Hi, that worked for me but I had to add the following to get rid of some error messages: import nest_asyncio nest_asyncio.apply() and to use shell command, i.e. See man page for details on internal buffering relating to '-u'Īlso, you might want to try change your loop to for line in iter(lambda: p.stdout.read(1), ''):, as this reads 1 byte at a time before processing. u : unbuffered binary stdout and stderr also PYTHONUNBUFFERED=x Try adding -u parameter to Python to run the process as unbuffered: The rver module probably buffers the output. So when your program gets to this line, it waits for data and process it.Īs your code works, but when run as a model not, it has to be related to this somehow.
What happens when there is no data? it waits. Think of how tail -f works on linux: it waits until something is written to the file, and when it does it echo's the new data to the screen. Once something is in it, you get the data and execute the inner part. When you are reading from an empty buffer, you are blocked until something is written to that buffer. How for line in p.stdout is been evaluated internally? is it some sort of endless loop till reaches stdout eof or something? With Popen(cmd, stdout=PIPE, stderr=STDOUT, bufsize=1) as p:Ĭmd2 = from subprocess import Popen, PIPE, CalledProcessError, run, STDOUT import os It just redirects the stderr to stdout and only stdout is read. # wait either for stderr or stdout and loop over the resultsįor line in asyncio.as_completed():īased on your example this is a really simple solution. Print('process started '.format(proc.pid)) Proc = await asyncio.create_subprocess_exec( # start the webserver without buffering (-u) and stderr and stdin as the arguments
#PYTHON SUBPROCESS GET OUTPUT IN REAL TIME WINDOWS#
In the background asyncio is using IOCP - a windows API to async stuff. Later I found a simpler solution, so you can choose, both of em should work. My first attempt was to use asyncio, a nice API, which exists in since Python 3.4. I think the main problem is that rver somehow is logging the output to stderr, here I have an example with asyncio, reading the data either from stdout or stderr. The above snippet is code from this answer and I'm running rver from a virtualenv (python3.6.2-32bits on win7) This topic has already been addressed few times here in SO but I haven't found a windows solution. However, If I run execute(cmd2) instead nothing will be printed, why is that and how can I fix it so I could see the rver's output in real time.Īlso, how for line in p.stdout is been evaluated internally? is it some sort of endless loop till reaches stdout eof or something? If I run execute(cmd1) the output will be printed without any problems. Raise CalledProcessError(p.returncode, p.args)
With Popen(cmd, shell=True, stdout=PIPE, bufsize=1, universal_newlines=True) as p: Given this code snippet: from subprocess import Popen, PIPE, CalledProcessError