Get standard output in real time with Python subprocess

Acquires the standard output of the command executed by subprocess asynchronously line by line.

import sys
import subprocess


def get_lines(cmd):
    '''
    :param cmd:str command to execute.
    :rtype: generator
    :return:Standard output(Line by line).
    '''
    proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    
    while True:
        line = proc.stdout.readline()
        if line:
            yield line
        
        if not line and proc.poll() is not None:
            break


if __name__ == '__main__':
    for line in get_lines(cmd='du ~/'):
        sys.stdout.write(line)

Recommended Posts

Get standard output in real time with Python subprocess
Output log in JSON format with Python standard logging
Make standard output non-blocking in Python
Get started with Python in Blender
Get YouTube Live chat field in real time with API
Get additional data in LDAP with python
Convert to a string while outputting standard output with Python subprocess
Read the standard output of a subprocess line by line in Python
UnicodeEncodeError struggle with standard output of python3
Get Started with TopCoder in Python (2020 Edition)
How to get the date and time difference in seconds with python
Get and convert the current time in the system local timezone with python
Get time series data from k-db.com in Python
[Python] Get the files in a folder with Python
Read the output of subprocess.Popen in real time
How to output "Ketsumaimo" as standard output in Python
Try converting videos in real time with OpenCV
Output python execution time
Get date in Python
Japanese output in Python
Get date with python
python get current time
Visualize accelerometer information from the microcomputer board in real time with mbed + Python
Get all standard inputs used in paiza and competitive programming with int (python)
[Python] Get the numbers in the graph image with OCR
Output the time from the time the program was started in python
Get the result in dict format with Python psycopg2
Use communicate () when receiving output in a Python subprocess
Japanese output when dealing with python in visual studio
Get in touch with functional programming in JavaScript or Python 3
[Super easy] Simultaneous face recognition and facial expression recognition in real time with Python and OpenCV!
Get country code with python
Scraping with selenium in Python
Get last month in python
Working with LibreOffice in Python
Get Twitter timeline with python
Scraping with chromedriver in python
Standard input with time limit
Debugging with pdb in Python
Try Python output with Haxe 3.2
Test standard output with Pytest
Working with sounds in Python
Scraping with Selenium in Python
Scraping with Tor in Python
Transposed matrix in Python standard
Tweet with image in Python
Get Terminal size in Python
Explicitly get EOF in python
Execution time measurement with Python With
Combined with permutations in Python
Get thread ID with python
Get started with Python! ~ ② Grammar ~
Get stock price with Python
Get Evernote notes in Python
Get home directory with python
Get keyboard events with python
Time synchronization (Windows) with Python
Get Alembic information with Python
Read Fortran output in python
Get Japanese synonyms in Python
Try converting webcam / camcorder videos in real time with OpenCV