Start background task independent of connection on Heroku #1820
-
I'm running into a stumbling block while trying to deploy an app to Heroku. I've reproduced it using the app.py script in the example. I've moved the start_background_task call as you suggested here https://stackoverflow.com/questions/53379228/flask-socketio-how-to-share-data-between-python-thread-and-socketio-start-backg. I commented out start_background_task call in the connect() function so I have it as below:
This works when deployed locally but not when deployed to Heroku. Locally, it emits and the count continues up regardless of connection as expected. On Heroku, the connection message emits but the server event never fires. I'm using a runtime.txt file to use Python 3.8: My requirements.txt file is as follows:
And finally my Procfile is this: I'm probably missing something, but cannot seem to figure out what. I have a couple of threads that I want to start immediately rather than wait for connections to fire them. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
It does not work because you are running Gunicorn. The code in the |
Beta Was this translation helpful? Give feedback.
It does not work because you are running Gunicorn. The code in the
if __name__ == '__main__'
conditional never executes when using Gunicorn. The easiest way to make this work is to run on the Gevent web server by runningpython app.py
, like you are doing locally.