Hi I have spent a few days reading at stackoverflow for an answer why I get the output listed below instead of seeing the loop in hello.py count forever. Still unclear for me and nothing works. How to start a few scripts in POpen subprocesses and get their output continously. Not when they have finished, because they will run forever. In case this is considered already answered, I'd be glad if someone can link to something functional with Python3.
I run this .py code in the hope to see hello.py execute:
import subprocess
import sys
command = [sys.executable, 'hello.py']
process_handle = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
while process_handle.poll() is None:
for line in iter(process_handle.stdout.readline, ""):
output = line.decode('utf-8')
if output != '':
print('Last number is: ' + output)
Here is what I get. No running of hello.py:
Python 3.7.3 (default, Apr 3 2019, 05:39:12) [GCC 8.3.0] on linux Type "help", "copyright", "credits" or "license" for more information.
hello.py contains:
i = 0
while True:
print(i)
i=i+1
sleep(1)
I tried the first variant from the second duplicate suggestion from live output from subprocess command
It does not compile/execute:
import subprocess
import sys
with open('test.log', 'w') as f: # replace 'w' with 'wb' for Python 3
process = subprocess.Popen(your_command, stdout=subprocess.PIPE)
for c in iter(lambda: process.stdout.read(1), ''): # replace '' with b'' for Python 3
sys.stdout.write(c)
f.write(c)
Result:
Traceback (most recent call last): File "hello.py", line 11, in Traceback (most recent call last): File "hello5.py", line 7, in sys.stdout.flush() NameError: name 'sys' is not defined sys.stdout.write(c) TypeError: write() argument must be str, not bytes
Now I tried the second variant from above, modified like this:
import subprocess
import sys
command = [sys.executable, 'hello.py']
process = subprocess.Popen(command, stdout=subprocess.PIPE)
for line in iter(process.stdout.readline, b''): # replace '' with b'' for Python 3
sys.stdout.write(line)
Result:
Traceback (most recent call last): File "hello.py", line 11, in Traceback (most recent call last): File "hello5.py", line 8, in sys.stdout.flush() NameError: name 'sys' is not defined sys.stdout.write(line) TypeError: write() argument must be str, not bytes
After advice from ShadowRanger I added b'' for binary, sys was already imported however:
import subprocess
import sys
command = [sys.executable, 'hello.py']
process = subprocess.Popen(command, stdout=subprocess.PIPE)
for c in iter(lambda: process.stdout.read(1), b''): # replace '' with b'' for Python 3
sys.stdout.write(c)
Result, despite adding import sys, sys is not defined? Plus now it complains about binary and wants string.
Traceback (most recent call last): File "hello.py", line 11, in sys.stdout.flush() NameError: name 'sys' is not defined Traceback (most recent call last): File "hello5.py", line 8, in sys.stdout.write(c) TypeError: write() argument must be str, not bytes
OK, I went with ShadowRanger's info and converted the binary output to text again. hello.py is now printing text.
sys.stdout.write(c.decode('ASCII'))
BUT its coming all at once and thats not what I need. Every printed line in hello.py must show up in realtime and not just when the script finishes.