1

I have a Python script I run from a Docker container and I have it print out what it's doing so that I can view that in docker logs -f <container>. The logs do print...but they seem to print in "batches" as opposed to real-time. They used to show up in real-time and I don't know what has changed.

The relevant part of the script:

point = f"temp value={temp}"
topic = f"lp/things/{client_name}/temp"
print(f"Publishing to topic: {topic}\n\tPayload: {point}")
(rc, mid) = client.publish(topic, point, 2, retain=True)
time.sleep(2)

I've confirmed that it is publishing on the 2-second schedule it's supposed to by looking at the broker it's publishing to.

Here is the Dockerfile that creates the image I run:

# syntax=docker/dockerfile:1
FROM python:3.8-slim-buster
ADD requirements.txt /
ADD mqtt_client.py /
RUN pip3 install -r requirements.txt
CMD ["/mqtt_client.py"]
ENTRYPOINT ["/usr/local/bin/python3"]

Any ideas?

Sam Dillard
  • 670
  • 1
  • 5
  • 18
  • 2
    https://stackoverflow.com/questions/17610870/why-is-my-python-output-delayed-to-the-end-of-the-program/17610891 – Gambit Support Sep 14 '21 at 15:58
  • @GambitSupport does it matter that I'm using `print` as opposed to `sys.write()`? Also, this isn't an issue outside of Docker. When I run it directly on the host, it prints at the expected cadence. Is this just a Docker quirk? – Sam Dillard Sep 14 '21 at 16:10
  • stdout buffering is non-deterministic. Google is your friend. It gives me https://stackoverflow.com/questions/5574702/how-to-print-to-stderr-in-python – Gambit Support Sep 14 '21 at 16:18

0 Answers0