Resolved with the -u
option
docker-compose.yml
command: python -u main.py
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
docker-compose run
docker-compose run python bash
Eventually I commented out the python script line by line and finally found that the following while True:
seems to be the problem. [^ 2]
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