Read the standard output of a subprocess line by line in Python

Overview

I think that you may create a child process using the subprocess module, but you may want to do something like monitoring the log of that, so make a note.

Child process

The number is output as standard every 0.5 seconds.

child.py


import time
import sys
i = 0
while True:
   print i
   sys.stdout.flush()
   i += 1
   time.sleep(0.5)

The point is to flush () and expel the data stored in the buffer.

Parent process

Whenever a child outputs, it will be output.

parent.py


import subprocess
proc = subprocess.Popen(['python','child.py'],stdout=subprocess.PIPE)

print "ready"
for line in iter(proc.stdout.readline,''):
   print line

result

ready
1
2
3
4
5
(The following is omitted)

The part to be read from the child process seems to be various if you create a thread and read it asynchronously and queue the data.

Recommended Posts

Read the standard output of a subprocess line by line in Python
Read the file line by line in Python
Read the file line by line in Python
Output in the form of a python array
Various ways to read the last line of a csv file in Python
Change the standard output destination to a file in Python
[Python] Read the specified line in the file
Output the number of CPU cores in Python
Get the caller of a function in Python
Make a copy of the list in Python
Read the output of subprocess.Popen in real time
Read line by line from a file with Python
Use communicate () when receiving output in a Python subprocess
Group by consecutive elements of a list in Python
A reminder about the implementation of recommendations in Python
Read Fortran output in python
[Python] Change the text color and background color of a specific keyword in print output
Find out the apparent width of a string in python
Get the number of specific elements in a python list
[Note] Import of a file in the parent directory in Python
Google search for the last line of the file in Python
Find the eigenvalues of a real symmetric matrix in Python
Make standard output non-blocking in Python
Have python read the command output
__init__ called by wxPython or Tkinter was a __init__ call of the inheriting class in Python
What seems to be a template of the standard input part of the competition pro in python3
Convert to a string while outputting standard output with Python subprocess
[Python] A memo of frequently used phrases (by myself) in Python scripts
How to determine the existence of a selenium element in Python
A simple data analysis of Bitcoin provided by CoinMetrics in Python
[Understanding in the figure] Management of Python virtual environment by Pipenv
How to check the memory size of a variable in Python
Output the contents of ~ .xlsx in the folder to HTML with Python
The story of making a standard driver for db with python.
How to check the memory size of a dictionary in Python
A function that measures the processing time of a method in python
Read a file in Python with a relative path from the program
Get the number of readers of a treatise on Mendeley in Python
Check the behavior of destructor in Python
Output tree structure of files in Python
Write the test in a python docstring
Display a list of alphabets in Python 3
Run the Python interpreter in a script
The result of installing python in Anaconda
[python] [meta] Is the type of python a type?
The basics of running NoxPlayer in Python
UnicodeEncodeError struggle with standard output of python3
Pandas of the beginner, by the beginner, for the beginner [Python]
I tried to summarize the contents of each package saved by Python pip in one line
In search of the fastest FizzBuzz in Python
The story of blackjack A processing (python)
If you want a singleton in python, think of the module as a singleton
[Python] How to put any number of standard inputs in a list
Check the in-memory bytes of a floating point number float in Python
Test & Debug Tips: Create a file of the specified size in Python
Receive a list of the results of parallel processing in Python with starmap
Get a datetime instance at any time of the day in Python
I made a program to check the size of a file in Python
[Implementation example] Read the file line by line with Cython (Python) from the last line
How to sort by specifying a column in the Python Numpy array.
Draw a graph of a quadratic function in Python