This document describes an older version of Celery (2.1). For the latest stable version please go here.

Task Sets, Subtasks and Callbacks - celery.task.sets

class celery.task.sets.TaskSet(task=None, tasks=None)

A task containing several subtasks, making it possible to track how many, or when all of the tasks has been completed.

Parameters:tasks – A list of subtask instances.
total

Total number of subtasks in this task set.

Example:

>>> from djangofeeds.tasks import RefreshFeedTask
>>> from celery.task.sets import TaskSet, subtask
>>> urls = ("http://cnn.com/rss",
...         "http://bbc.co.uk/rss",
...         "http://xkcd.com/rss")
>>> subtasks = [RefreshFeedTask.subtask(kwargs={"feed_url": url})
...                 for url in urls]
>>> taskset = TaskSet(tasks=subtasks)
>>> taskset_result = taskset.apply_async()
>>> list_of_return_values = taskset_result.join()
Publisher

alias of TaskPublisher

apply()

Applies the taskset locally.

apply_async(*args, **kwargs)

Run all tasks in the taskset.

Returns a celery.result.TaskSetResult instance.

Example

>>> ts = TaskSet(tasks=(
...         RefreshFeedTask.subtask(["http://foo.com/rss"]),
...         RefreshFeedTask.subtask(["http://bar.com/rss"]),
... ))
>>> result = ts.apply_async()
>>> result.taskset_id
"d2c9b261-8eff-4bfb-8459-1e1b72063514"
>>> result.subtask_ids
["b4996460-d959-49c8-aeb9-39c530dcde25",
"598d2d18-ab86-45ca-8b4f-0779f5d6a3cb"]
>>> result.waiting()
True
>>> time.sleep(10)
>>> result.ready()
True
>>> result.successful()
True
>>> result.failed()
False
>>> result.join()
[True, True]
task
task_name
tasks
class celery.task.sets.subtask(task=None, args=None, kwargs=None, options=None, **extra)

Class that wraps the arguments and execution options for a single task invocation.

Used as the parts in a TaskSet or to safely pass tasks around as callbacks.

Parameters:
  • task – Either a task class/instance, or the name of a task.
  • args – Positional arguments to apply.
  • kwargs – Keyword arguments to apply.
  • options – Additional options to celery.execute.apply_async().

Note that if the first argument is a dict, the other arguments will be ignored and the values in the dict will be used instead.

>>> s = subtask("tasks.add", args=(2, 2))
>>> subtask(s)
{"task": "tasks.add", args=(2, 2), kwargs={}, options={}}
apply(args=(), kwargs={}, **options)

Apply this task locally.

apply_async(args=(), kwargs={}, **options)

Apply this task asynchronously.

delay(*argmerge, **kwmerge)

Shortcut to apply_async(argmerge, kwargs).

get_type()

Previous topic

Defining Tasks - celery.task.base

Next topic

Executing Tasks - celery.execute

This Page