An extension for celery to dispatch large amount of subtasks within a main task, and process the results separately.
pip install celery-dispatcher
NOTICE:
celery-dispatcher
use tls to store running info, so the pool implementation using coroutines(like eventlet/gevent) can not be used
Firstly, yield subtask and its parameters in tuple from the main task by following order:
task
: signatureargs
: tupple/listkwargs
: dictoptions
: dict
Then register the result handler for each subtask using signal:
from celery import shared_task
from celery_dispatcher import dispatch
from celery_dispatcher.signals import subtask_success
@shared_task
def sqrt(i):
return i * i
@dispatch
@shared_task
def calc():
for i in range(10):
yield sqrt, (i,)
@subtask_success.connect(sender=calc)
def handle_result(root_id, task_id, retval, **kwargs):
print(retval)
Or register in the decorator directly:
from celery import shared_task
from celery_dispatcher import dispatch
@shared_task
def sqrt(i):
return i * i
def handle_result(root_id, task_id, retval, **kwargs):
print(retval)
@dispatch(receiver=handle_result)
@shared_task
def calc():
for i in range(10):
yield sqrt, (i,)
If you want to revoke all subtasks, call revoke with specific signal:
import signal
from celery import shared_task
from celery_dispatcher import dispatch
@shared_task
def subtask(i):
...
@dispatch
@shared_task
def maintask():
for i in range(10):
yield subtask, (i,)
result = maintask.delay()
result.revoke(terminate=True, signal=signal.SIGUSR1)
The dispatch
accepts the following parameters:
options
: dictreceiver
: callablebackend
: strauto_ignore
: bool
Default: No result backend enabled by default.
The backend used to store subtask info. Can be one of the following:
- redis: Use Redis to store the results. See Redis backend settings.
Default: 100
The batch size of subtask dispatching, or the result retrieving.
Default: 100
The queue size of subtasks in result retrieving. celery-dispatcher
put the unfinished subtasks into a queue, polling it continuously for completion of subtasks.
Default: 10
The default timeout for polling subtasks.
Default: 3600
The default timeout in seconds before celery-dispatcher
gives up retrieving the result of each subtask.
Default: False
Whether raise exception when subtask timeout.
Default: False
Whether raise exception from subtask.
Default: 1
The number of steps to record. celery-dispatcher
only actually store the updated progress in the background at most every N steps.