Search code examples
djangocelerydjango-celery

How to execute tasks one per second?


I have this:

eta_date = date.today()
eta = datetime.combine(eta_date, time.max)
scheduled_task.apply_async(eta=eta)

scheduled_tasks:

@task
def scheduled_task():
    for obj in ModelData.objects.all():
        send_data(obj)

send_data function sends object to other server as JSON. I use Celery. I want to start task on end of the day but in such a way that one of the objects is sent once per second. How to do it?


Solution

  • allcaps already told you the answer in the comment section, but it's what I would have answered anyway. Just add a sleep after send_data to wait X seconds.

    import time
    
    @task
    def scheduled_task():
        for obj in ModelData.objects.all():
            send_data(obj)
            time.sleep(1)  # You can also use a float here if 1 second is too long