Question

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?

Était-ce utile?

La 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

Autres conseils

Another option could be to spawn a task per obj in ModelData and set a limit on it to 1s.

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

@task(rate_limit='1/s')
def send_data_task(obj):
    send_data(obj)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top