How do I start background threads when launching a Flask app through gunicorn?

Dmiters

I'm launching my Flask app directly through the interpreter.

if __name__ == '__main__':
    t = Thread(target=run_schedule)
    t.start()
    context = ('cert.pem', 'key.pem')
    app.run(host='0.0.0.0',port=8080,debug=False,ssl_context=context)

The run_schedule function loops forever, unblocking once in a while to do a task.

Apparently I'm supposed to use a wsgi server like gunicorn when I'm not debugging anymore, but it doesn't call the module through main(), so the thread doesn't start.

Putting them outside that block won't work because then the thread would get started if any other code imports the module!

@before_first_request is almost what I need, but it requires me to poke the server with a request first. Not ideal.

What's the recommended way to do it? (Or do background threads go against wsgi philosophy?)

Nils Werner

You shouldn't spawn background threads in your server application. For example a WSGI server might spawn several server apps and then you have several background threads. Instead look into cronjobs or job queues like Celery.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Flask, Nginx, Gunicorn Stack Launching Selenium instance

How do i start multiple threads in a for loop?

NullPointerException when launching web start app

Launching background threads on GWT startup

How do I run a flask app in gunicorn if I used the application factory pattern?

How do I yield to another request when using gunicorn with asyncio?

Loading an eventstream through Gunicorn + Flask

How to run Flask app using gunicorn?

Calabash - start_test_server_in_background not launching app

How do I setup logging when using aiohttp and aiopg with Gunicorn?

Unsuccessful detection of first app launching , how should I do it?

Why do native threads behave different when app is in background?

How can I correctly run a timed process in my app the will start on stop when the application is in use or in the background?

How to get Flask app running with gunicorn

How run a flask app packaged as pex in gunicorn?

how to run a flask/gunicorn app as a systemd service

How do I allow all subseqent threads to continue through a "gate"

How can I store the spark's log output when launching in my local machine through intellij?

How can I start an asynchronous function in Flask / gunicorn / gevent?

How to serve flask app through nginx using gunicorn on a custom domain?

how do I stop a timer when the app gets into the background or user exits the app

How do I diagnose problems in a Python Flask application that only occur when app is run with uWSGI?

How do I fix this Elastic Beanstalk error when deploying a Flask app: Python 3.7.0 was not found on your system

How do I start the animation of a block when you scrolled through it in react-spring?

How do I start Deluge in the background?

How do I step through Cordova code when the app starts?

Do I need service to do something when app is in background?

How do I assign WM_CLASS when launching an application

ImportError occurs when launching flask application using `gunicorn -c ` command