문제

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?

도움이 되었습니까?

해결책

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

다른 팁

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)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top