I'm trying to understand how celery works
In my django application in tasks.py
file I have created one task:
@celery.shared_task(default_retry_delay=2 * 60, max_retries=2)
def my_task(param1, param2):
# There are some operations
I call this task by using this code:
my_task.delay(param1, param2)
Inside of this my_task
there is one condition where this task should be started again but after one minute delay
I have found that there are some kind of ETA and countdown for tasks, but their examples are only with apply_async
Is it possible to use some kind countdown for delay
?
Celery provides two function call options, delay() and apply_async() , to invoke Celery tasks. delay() has comes preconfigured and only requires arguments to be passed to the task — that's sufficient for most basic needs. Apply_async is more complex, but also more powerful then preconfigured delay.
The ETA (estimated time of arrival) lets you set a specific date and time that is the earliest time at which your task will be executed. countdown is a shortcut to set ETA by seconds into the future.
Celery tasks run asynchronously, which means that the Celery function call in the calling process returns immediately after the message request to perform the task is sent to the broker. There are two ways to get results back from your tasks.
To answer your opening questions: As of version 2.0, Celery provides an easy way to start tasks from other tasks. What you are calling "secondary tasks" are what it calls "subtasks".
From basic part of celery Calling documentation
delay(*args, **kwargs)
Shortcut to send a task message, but doesn’t support execution options.
So delay is clearly convenient, but if you want to set additional execution options you have to use apply_async.
As documentation states delay
cannot be used with additional options set so you should just convert your call into apply_async
If you want to add execution options, the docs suggest you use a signature. e.g:
my_task.s(arg1, arg2).apply_async(countdown=60)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With