Python log is not output with docker-compose up

Conclusion

Resolved with the -u option

docker-compose.yml


command: python -u main.py

Background

When I specified the following file in command of docker-compose.yml, nothing was output to the log. [^ 1]

[^ 1]: It took me a long time to notice that it was "not output" instead of "not working"

import time

print('hoge')

while True:
  print('fuga')
  time.sleep(1)

We will partially isolate it for investigating the cause.

[^ 2]: Sometimes it was output when I deleted the ʻimport` statement, but this just stopped working on that line due to an error

Eventually I commented out the python script line by line and finally found that the following while True: seems to be the problem. [^ 2]

Standard output and buffering

The standard output by Python's print () etc. is not output immediately but is buffered and output according to the timing.

This meant that the result was not passed to docker-compose, which receives the output, because while True: never finishes execution and the buffer never fills up.

python has an option -u to disable buffering, which I solved by specifying it.

reference: How to stop block buffering of standard output in Python 3-Qiita Mysterious behavior of head command and buffering of standard I / O --CUBE SUGAR CONTAINER

Recommended Posts

Python log is not output with docker-compose up
Unit test log output with python
Output Python log to console with GAE
Python environment with docker-compose
Output log in JSON format with Python standard logging
Python round is not strictly round
Try Python output with Haxe 3.2
Output large log with discord.py
Python list is not a list
[Python] What is a with statement?
Make apache log csv with python
Output to csv file with Python
Input / output with Python (Python learning memo ⑤)
[Note] Hello world output with python
Roughly speed up Python with numba
[python3] Implement debug log output function easily with logging and Click
python note: when easy_install is not available
[Python] Name Error: name'urlparse' is not defined
With PEP8 and PEP257, Python coding that is not embarrassing to show to people!
Output color characters to pretty with python
[Python] Round up with just the operator
python log
UnicodeEncodeError struggle with standard output of python3
PNG saving is slow with Python imageio.imwrite
When the log acquired by rsyslog is not updated after rotation with logrotate
Csv output from Google search with [Python]! 【Easy】
[Web development with Python] query is also redirect
I tried to output LLVM IR with Python
Log in to Yahoo Business with Selenium Python
Output python log to both console and file
What are you comparing with Python is and ==?
[Python] Expression (1,2) does not make tuples with parentheses
[CentOS8] How to output Python standard output to systemd log
FizzBuzz with Python3
Scraping with Python
Python is easy
Investigate the cause when an error is thrown when python3.8 is not found when using lambda-uploader with python3.8
Scraping with Python
Python with Go
python learning output
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
What is python
Bingo with python
Zundokokiyoshi with python
Python is instance
It is easy to execute SQL with Python and output the result in Excel
Excel with Python
Microcomputer with Python
Cast with python
What is Python
Procedure to load MNIST with python and output to png
Python> assert not np.isnan (loss_value),'Model diverged with loss = NaN'
Note that writing like this with ruby is writing like this with python
Why not create a stylish table easily with Python?
Get standard output in real time with Python subprocess