python> print> Redirected only at the end of processing?> Run with -u

Operation check


Raspberry Pi2 (raspbian)
MacOS X 10.8.5

Output in python

Prepare the following code

hello.py


#!/usr/bin/env python
import time

print "hello"

for x in range(0,60):
	print x
	time.sleep(1)

Execute as follows

$ ./hello.py > log.tmp

Do the following in another terminal

$ tail -f log.tmp

When doing the above, it seems that the contents of log.tmp are updated after 60 prints </ font>.

When viewed with stdout, it is displayed every second, but when it is output to a file, it is not output every second.

Output in bash

Prepare a bash script instead

test-exec


#!/usr/bin/env bash

for x in $(seq 1 60)
do
  echo $x
  sleep 1
done

If you execute this, the file will be output every second.

Question

How do you get a python print statement to print a file every second?

It may be possible to use a file write command other than print, but I have to find out how to combine it with file redirection.

It is regrettable that even if the temperature and humidity can be measured with DHT11, it cannot be seen with tail -f when outputting the file.

Solution

http://stackoverflow.com/questions/107705/disable-output-buffering

It seems that you can specify the -u option when running python.

hello.py


#!/usr/bin/env python -u
import time

print "hello"

for x in range(0,60):
	print x
	time.sleep(1)

The above specification #! / Usr / bin / env python -u worked on MacOSX. On the other hand, on raspberry Pi 2, I got an error like -u option is missing. I made #! / usr / bin / python -u and it worked.

Recommended Posts

python> print> Redirected only at the end of processing?> Run with -u
Python Basic Course (at the end of 15)
Send Gmail at the end of the process [Python]
Try to react only the carbon at the end of the chain with SMARTS
Remove specific strings at the end of python
Run the intellisense of your own python library with VScode.
Basics of binarized image processing with Python
Check the existence of the file with python
Drawing with Matrix-Reinventor of Python Image Processing-
The story of blackjack A processing (python)
Receive a list of the results of parallel processing in Python with starmap
Automation of creation of working hours table created at the end of the month with Selenium
Prepare the execution environment of Python3 with Docker
2016 The University of Tokyo Mathematics Solved with Python
[Note] Export the html of the site with python.
View the result of geometry processing in Python
Calculate the total number of combinations with python
Periodically notify the processing status of Raspberry Pi with python → Google Spreadsheet → LINE
Image processing? The story of starting Python for
This is the only basic review of Python ~ 1 ~
This is the only basic review of Python ~ 2 ~
This is the only basic review of Python ~ 3 ~
At the time of python update on ubuntu
Convert the character code of the file with Python3
[Python] Determine the type of iris with SVM
3. Natural language processing with Python 3-3. A year of corona looking back at TF-IDF
I tried to compare the processing speed with dplyr of R and pandas of Python
Extract the table of image files with OneDrive & Python
Coordinates of the right end of Label made with tkinter
Learn Nim with Python (from the beginning of the year).
Run with CentOS7 + Apache2.4 + Python3.6 for the time being
Take a peek at the processing of LightGBM Tuner
Destroy the intermediate expression of the sweep method with Python
[Chapter 5] Introduction to Python with 100 knocks of language processing
Visualize the range of interpolation and extrapolation with python
Calculate the regression coefficient of simple regression analysis with python
Examine the close processing of Python dataset (SQLAlchemy wrapper)
[Chapter 3] Introduction to Python with 100 knocks of language processing
[Chapter 2] Introduction to Python with 100 knocks of language processing
Summary of the basic flow of machine learning with Python
Tasks at the start of a new python project
Get the operation status of JR West with Python
Extract the band information of raster data with python
[Chapter 4] Introduction to Python with 100 knocks of language processing
Run Python with VBA
Run prepDE.py with python3
the zen of Python
Image processing with Python
Various processing of Python
Run iperf with python
How to insert a specific process at the start and end of spider with scrapy
I tried to find the entropy of the image with python
Try scraping the data of COVID-19 in Tokyo with Python
I tried "gamma correction" of the image with Python + OpenCV
3. Natural language processing with Python 5-1. Concept of sentiment analysis [AFINN-111]
The story of implementing the popular Facebook Messenger Bot with python
Unify the environment of the Python development team starting with Poetry
Get UNIXTIME at the beginning of today with a command
Calculate the square root of 2 in millions of digits with python
I wrote the basic grammar of Python with Jupyter Lab
Tank game made with python About the behavior of tanks