Sunday, 28 July 2019

Is it possible to run flask in a single process? (to work around apparent issue with ipdb & Docker ttys)

I have a flask app which I am running like this:

flask run --host=0.0.0.0

When I look at the process list I see this:

UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 23:48 pts/0        00:00:00 /bin/sh -c flask run --host=0.0.0.0
root         6     1  1 23:48 pts/0        00:00:01 /usr/local/bin/python /usr/local/bin/flask run --host=0.0.0.0
root         8     6  3 23:48 pts/0        00:00:02 /usr/local/bin/python /usr/local/bin/flask run --host=0.0.0.0

Three processes.

If I run using --without-threads I also the same three processes:

UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 00:28 pts/0    00:00:00 /bin/sh -c flask run --host=0.0.0.0 --without-threads
root         6     1  2 00:28 pts/0    00:00:02 /usr/local/bin/python /usr/local/bin/flask run --host=0.0.0.0 --without-threads
root         8     6  4 00:28 pts/0    00:00:04 /usr/local/bin/python /usr/local/bin/flask run --host=0.0.0.0 --without-threads

Is there a way to somehow run flask as a single process?

Motivation

The flask app in question is running inside a docker container. I would like to be able to set breakpoints using ipdb.

I have observed that if I set this in my docker-compose file:

    stdin_open: true
    tty: true

and run, instead of a flask app, a simple single-process python app...

$ docker exec -it bug_demo_bug_demo_1 bash
root@98245482089b:/opt/bug_demo/bug_demo# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 00:41 pts/0    00:00:00 /bin/sh -c python app.py
root         7     1 20 00:41 pts/0    00:00:00 python app.py

... and attach to the container while the app is at a breakpoint, I am able to drop into ibpd and use it normally – arrow keys and tab completion work properly.

But when I try do the same with the flask app (attach to the container while the app is waiting in a breakpoint), things do not work correctly.

Either I disable tty: true in docker-compose.yml, and can use use ipdb but without arrow keys and tab completion, OR I leave tty: true in place, but then cannot really use ipdb at all, b/c it appears the tty is attached to all three flask processes, causing everything other than single character commands to get garbled. (Although I can see with this set up that arrow keys and tab completion work.)

All of this leads me to believe that if I can find a way to run my flask app as a single process, I will be able to attach to the docker container and use ipdb as desired.

Is there some way to do this?



from Is it possible to run flask in a single process? (to work around apparent issue with ipdb & Docker ttys)

No comments:

Post a Comment