How to start a single background task when django server boots up?

Viewed 1090

I need to run a long running background task when the Django server is started. This task needs to run in an endless loop, or restarted at set intervals. I was looking at Threading, multi processing, django-background-tasks.

The problem with django-background-tasks is that if I just declare my task in tasks.py, it inserts a new task each time the server is restarted. So after a while, I have hundreds of tasks running, while I only need a single one.

The problem with Threads and multi processing, is that these tasks are not triggered at all, unless I do import tasks in views.py. In that case, they get triggered all the time, for example, python3 manage.py makemigrations triggers the task. I only want to run the task when the server is started.

How can I achieve this?

This is what I have now. This keeps inserting new tasks each time I restart the server, resulting in hundreds of parallel tasks.

tasks.py

REPEAT = 60*15

@background()
def process_transactions():
  ...

process_transactions(repeat=REPEAT)
0 Answers
Related