Rohit Rohit - 7 months ago 15
Python Question

how to use django-background-tasks

I am making a django application. To calculate the rank of the feeds based on lines and comment, I am trying to use django-background-tasks. the function I am using in nodes models is:

@background(schedule=60)
def get_score(self):
p = self.likes+self.comments # popularity
t = (now()-self.date).total_seconds()/3600 # age_in_hrs
# last_activity =
n = self.admin_score
score = (p/pow((t+1), 1.2))*n
self.score = score
return score


But I am not seeing any change in score. That means that I am doing it in a right way and i am missing the basic concept. Can somebody tell me how to use django-background-tasks to schedule task or refer me to some existing documents.

Answer

You should run python manage.py process_tasks as described here. You can add it to crontab to execute periodically.

UPD:

  1. You don't need to run process_tasks using crontab cause this command internally sleeps every 5 seconds (this value is configurable) and then again checks whether there is any task to run.
  2. Your task looks strange. You should declare it as global function in separate file and pass id of model inside it, fetch object by id do calculations and save your object.
Comments