0

I'm having some issues with my application, which is supposed to run a function in the background every 10 seconds and handle requests simultaneously. To do so, I created a daemonized thread with threading.Timer recursively:

from flask import Flask
from threading import Timer


def set_timer_and_start(i):
    print("Iteration", i)
    thread = Timer(10, set_timer_and_start, args=(i+1,))
    thread.daemon = True
    thread.start()


print("Starting app...")

app = Flask(__name__)

print("Starting thread")

set_timer_and_start(1)

The point is: this application is working when I'm running locally (directly on my machine).

 * Serving Flask app 'app.py' (lazy loading)
 * Environment: development
 * Debug mode: on
Starting app...
Starting thread
Iteration 1
 * Running on http://127.0.0.1:5000 (Press CTRL+C to quit)
^C% 

But when I try to run it inside a Docker container, it doesn't work, and I don't get any message on the terminal before stopping the container. When I stop it, all the messages appear in the terminal like this:

docker run -p 5000:5000 docker-image-test:latest
 * Serving Flask app '/usr/src/app/app.py' (lazy loading)
 * Environment: development
 * Debug mode: on
 * Running on http://127.0.0.1:5000 (Press CTRL+C to quit)
^CStarting app...
Starting thread
Iteration 1

Here is the Dockerfile

FROM python:3.10

RUN pip install flask

WORKDIR /usr/src/app

COPY ./app.py /usr/src/app/app.py

ENV FLASK_APP=/usr/src/app/app.py
ENV FLASK_ENV=development

CMD ["flask", "run", "--no-reload"]

I don't know if I'm doing something wrong, but what I want is to run a function in the background considering it's a Flask application and not necessarily related to the webserver (I need the webserver for other reasons, but it's also necessary to run this function in the background).

Edit: I guess I found the problem: it seems that the thread is executing but the messages are not shown (for some reason). If so, why does print not work at the moment it's called (or, at least, the message is not printed)?

akahenry
  • 1
  • 3
  • Is the problem that output isn't being printed on the container's output ([Python app does not print anything when running detached in docker](https://stackoverflow.com/questions/29663459/python-app-does-not-print-anything-when-running-detached-in-docker)), that the container is unreachable ([Deploying a minimal flask app in docker - server connection issues](https://stackoverflow.com/questions/30323224/deploying-a-minimal-flask-app-in-docker-server-connection-issues)), something else? – David Maze Jul 06 '22 at 19:45
  • It seems that the container is not executing the second thread before I try to stop the container. The output is just a way I tried to check this behavior. The container is reachable and I can access the webserver without problems. – akahenry Jul 06 '22 at 21:11
  • I guess I found the problem: it seems that the thread is executing but the messages are not shown (for some reason). If so, why does `print` not work at the moment it's called (or, at least, the message is not printed)? – akahenry Jul 06 '22 at 23:06
  • If you `docker run -e PYTHONUNBUFFERED=1 docker-image-test`, does the `print()` output appear immediately? That's the recommendation of the first question I linked to. – David Maze Jul 07 '22 at 01:07
  • It works! Thank you and sorry for the misunderstanding. – akahenry Jul 07 '22 at 16:53

0 Answers0