Option to disable stdout / stderr buffers in Python

I wrote this article because I sometimes forget it and see painful eyes.

If you run it as-is in the shell, the following command should work as expected, as it says. Output 1 and wait 3 seconds, then output 2.

$ python -c 'import time; print(1); time.sleep(3); print(2)'
1
2
$

If you pass a pipe through this, it will not come out unless the buffer size of the pipe is accumulated.

In the above example,

$ python -c 'import time; print(1); time.sleep(3); print(2)' | cat
1
2
$

I just piped it to cat, but the lines 1 and 2 come out at the same timing after 3 seconds. To put it like that, the one who says "the pipe is clogged". Even if it is written to a file, it is also written with a delay.

If you want to suppress this behavior, use the `` `-u``` option.

$ python -u -c 'import time; print(1); time.sleep(3); print(2)' | cat
1
2
$

↑ Wait 3 seconds after 1 comes out, then 2 comes out.

http://docs.python.jp/3/using/cmdline.html#cmdoption-u

pythonunbufferedYou can get the same effect by putting a non-empty string in the environment variable.

Recommended Posts

Option to disable stdout / stderr buffers in Python
To flush stdout in Python
Login to website in Python
Speech to speech in python [text to speech]
How to develop in Python
Post to Slack in Python
Read Protocol Buffers data in Python3
Convert markdown to PDF in Python
How to collect images in Python
How to use SQLite in Python
Disable python2.6 ssl3 protocol in centos6
In the python command python points to python3.8
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
6 ways to string objects in Python
How to use PubChem in Python
How to handle Japanese in Python
An alternative to `pause` in Python
I tried to implement PLSA in Python
[Introduction to Python] How to use class in Python?
Try logging in to qiita with Python
Install Pyaudio to play wave in python
I tried to implement permutation in Python
Method to build Python environment in Xcode 6
How to dynamically define variables in Python
How to do R chartr () in Python
Send email to multiple recipients in Python (Python 3)
I tried to implement PLSA in Python 2
To set default encoding to utf-8 in python
Decorator to avoid UnicodeEncodeError in Python 3 print ()
How to work with BigQuery in Python
Log in to Slack using requests in Python
How to get a stacktrace in python
How to display multiplication table in python
Easy way to use Wikipedia in Python
How to extract polygon area in Python
How to check opencv version in python
I tried to implement ADALINE in Python
[Python] Pandas to fully understand in 10 minutes
Throw Incoming Webhooks to Mattermost in Python
Module to generate word N-gram in Python
To reference environment variables in Python in Blender
I wanted to solve ABC159 in Python
I tried to implement PPO in Python
How to switch python versions in cloud9
How to adjust image contrast in Python
How to use __slots__ in Python class
How to dynamically zero pad in Python
Manage python packages to install in containers
To work with timestamp stations in Python
How to use regular expressions in Python
Convert from Markdown to HTML in Python
How to display Hello world in python
Adding Series to columns in python pandas
How to use is and == in Python
How to write Ruby to_s in Python
Introduction to Vectors: Linear Algebra in Python <1>
Convert absolute URLs to relative URLs in Python
Introduction to Effectiveness Verification Chapter 1 in Python
How to use the C library in Python