this is my code:
def enqueue_output(file, queue):
for line in iter(file.readline, ''):
queue.put(line)
file.close()
def read_popen_pipes(p):
with ThreadPoolExecutor(2) as pool:
q_stdout, q_stderr = Queue(), Queue()
pool.submit(enqueue_output, p.stdout, q_stdout)
pool.submit(enqueue_output, p.stderr, q_stderr)
while True:
if p.poll() is not None and q_stdout.empty() and q_stderr.empty():
break
out_line = err_line = ''
try:
out_line = q_stdout.get_nowait()
except Empty:
pass
try:
err_line = q_stderr.get_nowait()
except Empty:
pass
yield (out_line, err_line)
def handle_base():
try:
with subprocess.Popen(
['conda', 'run', '-n', 'information_security', 'python', '/data/lkl/front/snn_assess/resnet18.py'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
) as process:
for out_line, err_line in read_popen_pipes(process):
if(out_line != ''):
socketio.emit('task_output', {'output': out_line.strip()})
# Do stuff with each line, e.g.:
print(out_line, end='')
print(err_line, end='')
return process.poll()
I’ve tried every hot solution in the post: read subprocess stdout line by line
But none of them work.
like:
for line in io.TextIOWrapper(process.stdout, encoding="utf-8"): # or another encoding
# line = process.stdout.readline()
if not line:
break
print(line, end='')
socketio.emit('task_output', {'output': line.strip()})
And other methods also didn’t work.
It seems only when the subprocess terminates, can all the output be received at once, which is not what is expected(line by line).
I’m really confused, wondering the principle of python subprocess, and how to get the output in time line by line.
New contributor
Polynomial is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.