Task used internally by dmap_async() and TaskSet.map_async().
Execute an arbitrary function or object.
Note You probably want execute_remote() instead, which this is an internal component of.
The object must be pickleable, so you can’t use lambdas or functions defined in the REPL (that is the python shell, or ipython).
Parameters: |
|
---|
A periodic task is a task that behaves like a cron job.
Raises NotImplementedError: | |
---|---|
if the run_every attribute is not defined. |
You have to register the periodic task in the task registry.
Example
>>> from celery.task import tasks, PeriodicTask
>>> from datetime import timedelta
>>> class MyPeriodicTask(PeriodicTask):
... name = "my_periodic_task"
... run_every = timedelta(seconds=30)
...
... def run(self, **kwargs):
... logger = self.get_logger(**kwargs)
... logger.info("Running MyPeriodicTask")
>>> tasks.register(MyPeriodicTask)
A task that can be delayed for execution by the celery daemon.
All subclasses of Task must define the run() method, which is the actual method the celery daemon executes.
The run() method supports both positional, and keyword arguments.
Raises NotImplementedError: | |
---|---|
if the name attribute is not set. |
The resulting class is callable, which if called will apply the run() method.
Examples
This is a simple task just logging a message,
>>> from celery.task import tasks, Task
>>> class MyTask(Task):
...
... def run(self, some_arg=None, **kwargs):
... logger = self.get_logger(**kwargs)
... logger.info("Running MyTask with arg some_arg=%s" %
... some_arg))
... return 42
... tasks.register(MyTask)
You can delay the task using the classmethod delay()...
>>> result = MyTask.delay(some_arg="foo")
>>> result.status # after some time
'DONE'
>>> result.result
42
...or using the delay_task() function, by passing the name of the task.
>>> from celery.task import delay_task
>>> result = delay_task(MyTask.name, some_arg="foo")
Execute this task at once, by blocking until the task has finished executing.
Parameters: |
|
---|---|
Return type: |
Delay this task for execution by the celery daemon(s).
Parameters: |
|
---|---|
Return type: |
Delay this task for execution by the celery daemon(s).
Parameters: |
|
---|---|
Return type: |
Get a celery task message consumer.
Return type: | celery.messaging.TaskConsumer. |
---|
Please be sure to close the AMQP connection when you’re done with this object. i.e.:
>>> consumer = self.get_consumer()
>>> # do something with consumer
>>> consumer.connection.close()
Get process-aware logger object.
Get a celery task message publisher.
Return type: | celery.messaging.TaskPublisher. |
---|
Please be sure to close the AMQP connection when you’re done with this object, i.e.:
>>> publisher = self.get_publisher()
>>> # do something with publisher
>>> publisher.connection.close()
A task containing several subtasks, making it possible to track how many, or when all of the tasks has been completed.
Parameters: |
|
---|
Example
>>> from djangofeeds.tasks import RefreshFeedTask >>> taskset = TaskSet(RefreshFeedTask, args=[ ... [], {"feed_url": "http://cnn.com/rss"}, ... [], {"feed_url": "http://bbc.com/rss"}, ... [], {"feed_url": "http://xkcd.com/rss"}])>>> taskset_result = taskset.run() >>> list_of_return_values = taskset.join()
Gather the results for all of the tasks in the taskset, and return a list with them ordered by the order of which they were called.
Parameter: | timeout – The time in seconds, how long it will wait for results, before the operation times out. |
---|---|
Raises TimeoutError: | |
if timeout is not None and the operation takes longer than timeout seconds. |
If any of the tasks raises an exception, the exception will be reraised by join().
Returns: | list of return values for all tasks in the taskset. |
---|
Distribute processing of the arguments and collect the results asynchronously.
Returns: | celery.result.AsyncResult instance. |
---|
Run all tasks in the taskset.
Returns: | A celery.result.TaskSetResult instance. |
---|
Example
>>> ts = TaskSet(RefreshFeedTask, [
... ["http://foo.com/rss", {}],
... ["http://bar.com/rss", {}],
... )
>>> result = ts.run()
>>> 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]