Gunicorn: Running gunicorn with threading

Created on 24 Apr 2020  路  4Comments  路  Source: benoitc/gunicorn

I'm building a server with Flask/Gunicorn and Nginx. My script (Flask server) does two things with 'threading':

  1. connect to MQTT broker
  2. run a flask server

But when i try using gunicorn: gunicorn --bind 0.0.0.0:5000 wsgi:app, the first thread doens't run.

Here is the code (not complet):

import threading

def run_mqtt():
    while True:
        mqtt_client.connect(mqtt_server, port=mqtt_port)

def run_server():
    app.run(host='0.0.0.0', port=5000, debug=False)



if __name__ == '__main__':
    t1 = threading.Thread(target=run_mqtt)
    t2 = threading.Thread(target=run_server)
    t1.daemon = True
    t2.daemon = True
    t1.start()
    t2.start()

Should i use gevent to make it work?

All 4 comments

Running a WSGI application with gunicorn is not like running a script on the command line. The module's name won't be __main__ and so code hidden in an if __name__ == '__main__' block won't run. Based on what you've shown here, that looks like the problem.

For testing without Gunicorn and deploying with Gunicorn you could try this approach:

  • Create a function to perform your startup tasks.
  • Call that function from your if __name__ == '__main__': block.
  • Also call that function from a gunicorn post_worker_init hook.
# file my_app.py

def start_mqtt():
    t = threading.Thread(target=run_mqtt)
    t.daemon = True
    t.start()


if __name__ == '__main__':
    start_mqtt()
    app.run(host='0.0.0.0', port=5000, debug=False)


# file gunicorn.conf.py

def post_worker_init(worker):
    import start_mqtt from my_app
    start_mqtt()

Try starting from something like this and see if it helps.

no activity since awhile closing the issue. Thanks @jamadden @tilgovi for the answers.

Thanks a ton. I've been banging my head to implement threading with gunicorn. The post_worker_init hook worked flawlessly.

Thank you so much @tilgovi

Was this page helpful?
0 / 5 - 0 ratings