Threading in Flask not working with UWSGI but working on commandline
Asked Answered
D

2

8

I have a Flask application that works fine when run on the commandline, but when it is run through uWSGI it doesn't respond correctly to requests or the worker thread doesn't work properly. I've rewritten a simple proof-of-concept/failure program that demonstrates the issue:

from datetime import datetime
from threading import Event, Thread

from flask import Flask


class JobManager:
    def __init__(self):
        self.running = False
        self.event = Event()

    def start(self):
        self.running = True
        while self.running:
            print("Processing Job at", datetime.now().strftime('%c'))
            self.event.clear()
            self.event.wait(5)
            if self.event.is_set():
                print("Interrupted by request!")

    def stop(self):
        self.running = False
        self.event.set()


app = Flask(__name__)
jobs = JobManager()

t = Thread(target=jobs.start)
t.start()

@app.route('/')
def hello_world():
    global jobs
    jobs.event.set()

    return "I'm alive at " + datetime.now().strftime('%c')


if __name__ == '__main__':
    app.run()

I'd expect that calling the / route would print "Interrupted by request!" on the console but it just hangs even though the job should be running in a seperate thread.

My uWSGI configuration is:

[uwsgi]
module = app:app

master = true
processes = 5
threads = 2

socket = 0.0.0.0:5000
protocol = http

reload-mercy = 5
worker-reload-mercy = 5

die-on-term = true
enable-threads = true
thunder-lock = true

logto = /home/user/dev/flask-thread/uwsgi_log.log
logto2 = /home/user/dev/flask-thread/uwsgi2_log.log

env = PATH=/home/user/dev/flask-thread/env/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

I then run uWSGI in the venv with: uwsgi --ini uwsgi-test.ini

It will work if I use python app.py and use the built in flask development server.

My only guess is that it has something to do with the GIL interacting with uWSGI but that's a wild guess and I have no idea how to stop that.

Dimitris answered 20/8, 2018 at 12:56 Comment(0)
D
10

For me using the uwsgi flag --lazy worked.

uwsgi foo.ini --socket=0.0.0.0:5000 --protocol=http --lazy -w wsgi

Explanation:

Quote from http://lists.unbit.it/pipermail/uwsgi/2011-June/002307.html:

single process (no master):

the process creates the thread in which the WSGI callable put data.

This is ok

with master process:

the master load the app and generate the thread. Then it forks one or more workers. This workers are new processes with no relation with the thread spawned in the master. So your data will go nowhere (only the master can access the initial thread).

You have two way to follow:

1) add --lazy to the command line, in this way your app will be loaded after master's fork, so each worker will get its thread.

2) rewrite your app to use the uwsgi.post_fork_hook feature.

Dyak answered 5/10, 2018 at 13:0 Comment(3)
I ended up using the uwsgi.post_fork_hook and it works great now :) Thanks for your help. It was quite confusing to me how it splits off the workers and what needs to do what.Dimitris
you made my day! --lazy works. Finally uwsgi handles his side of the work nicely, which is to reconnect/recreate the sysPipe whenever is required.Inharmonic
The --lazy option is discouraged by the documentation: uwsgi-docs.readthedocs.io/en/latest/ThingsToKnow.html Beware as there is an older options named lazy that is way more invasive and highly discouraged (it is still here only for backward compatibility)Affricate
W
2

For me, lazy didn't work, and I didn't want to go through the hassle of using the post_fork_hook feature, but I did find the following on https://uwsgi-docs.readthedocs.io/en/latest/ThingsToKnow.html :

By default the Python plugin does not initialize the GIL. This means your app-generated threads will not run. If you need threads, remember to enable them with enable-threads. Running uWSGI in multithreading mode (with the threads options) will automatically enable threading support. This “strange” default behavior is for performance reasons, no shame in that.

No shame, indeed, but a heartache nonetheless. Long story short, I set enable-thread = true in my config file, and my threads began spinning off immediately and reliably

Wieland answered 10/3, 2022 at 21:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.