Home > Back-end >  Heroku multi-threading
Heroku multi-threading

Time:02-06

I am making a website with heroku and now I'm making a task that will run in the background
Something like this:

def task():
    while True:
        ...
        # some task that will run in the background

And in my file app.py, I have this code:

if __name__ == "__main__":
    Thread(target=app.run).start()
    Thread(target=task).start()

I've put some print in the task function (something like print("Task started")
But in the heroku log I only saw flask started message and no task message, not even an error, and the things that should happen in the task function doesn't happen. And I try to run the flask app in my local machine and it works.

So how to fix this?

CodePudding user response:

The "correct" way to do this on Heroku is to define your background task as a separate process, e.g. by doing something like this in your Procfile:

web: gunicorn app:app
worker: python path/to/background_task.py

Now you can scale up a dyno to run the worker process:

heroku ps:scale worker=1

There are lots of benefits to this approach, but it does mean you need to have at least two dynos running. That either costs money or consumes free dyno hours at a faster rate.

  •  Tags:  
  • Related