As is known to all, Celery have a already provides periodic task and it's very perfit. But, Assume this case: After my task was called, I hope it's task can frequency of execution, and when celery task was started.
The above case, Periodic task is hard to practice, Becacuse it's depend on celery beat. celery-task-tigger
do it over simple packaging or implement for solution to do it.
pip install celery-task-tigger
Assume you have aleady install celery and can do it.
Option max_times
is must be appoint.
from celery_tasktigger.decorator import tigger_task
@app.task(bind=True)
@tigger_task(max_times='forever') # forever is expressed unlimited time
def add(self, x, y):
return x + y
Option max_times
: The maximum number of execute the task times.
Type: int
Note: The value 'forever' is expressed unlimited time.
Example:
@app.task(bind=True)
@tigger_task(max_times=3) # after execute 3 times, raise an exception
def add(self, x, y):
return x + y
Option countdown
: You can also provide the countdown argument to execute.
Type: int
Default: 1 (seconds)
Example:
@app.task(bind=True)
@tigger_task(max_times='forever', countdown=3) # each execute in 3 seconds
def add(self, x, y):
return x + y
OR
@app.task(bind=True)
@tigger_task(max_times='forever')
def add(self, x, y, countdown=3): # you also can define formal parameter in task
return x + y
>> from example import add
>> add.apply_async((1,2))
you can also delayed execute task, as follow:
>> from example import add
>> add.apply_async((1,2), countdown=4) # after 4 seconds, begin start task
# add.apply_async((1,2),{'countdown': 2} countdown=4) ## after 4 seconds, begin start task and interval in 2 seconds
About Celery Task, Please see below for details: Celery Calling-Tasks Document
if you appoint max_times='forever'
or provides the bigger values of max_times, you must stop it in programe.
>> result = add.apply_async((1,2))
>> result.revoke()
or
>> from mycelery import app
>> app.control.revoke('task_id')
See below for details: Celery Document——FAQ
-
100% full compatible with Celery 3 | 4
-
the frequency of execution for task
-
...and many other stuff (o,0)
- Boyle Gu