I'm building a server with Flask/Gunicorn and Nginx. My script (Flask server) does two things with 'threading':
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?
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:
if __name__ == '__main__':
block.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