Lesnie Sncheider
Lesnie Sncheider

Reputation: 375

Run background task on repeat every N

I'm using django and I want a loop to run in the background let's say every 60 seconds. I found a plugin for Django which seems like it got what I need, but i'm not sure how to make it work. I understand you put the @background in, but as far as I understand django. This goes into a view right? But before the code is being loaded the first time, doesn't the webpage have to be loaded first?

I'm talking about the following plugin

https://django-background-tasks.readthedocs.io/en/latest/

Upvotes: 0

Views: 949

Answers (2)

Patrick Scott Best
Patrick Scott Best

Reputation: 153

If you'd like to go in a new direction and if this is an asynchronous command that you are running periodically, consider using celery with module celery-beat.

Upvotes: 0

Ralf
Ralf

Reputation: 16495

Just to be clear:

  • You need to install the app (pip install ..., adding to INSTALLED_APPS, running migrate)
  • You need to define a (or some) function which you decorate with @background
  • You need to call these decorated functions, maybe in a view or somewhere else. This will add the function to a queue (a table in the database, which was created when running migrate).
  • You need to run python manage.py process_tasks as a regular cron job; this will read the queue and execute the functions. As long as you don't call this, no function will be executed and queue will just keep filling.

Does this answer your question? Or did I misunderstand you?

Upvotes: 1

Related Questions