Executing Tasks - celery.execute

class celery.execute.ExecuteWrapper(fun, task_id, task_name, args=None, kwargs=None)

Wraps the task in a jail, which catches all exceptions, and saves the status and result of the task execution to the task meta backend.

If the call was successful, it saves the result to the task result backend, and sets the task status to "DONE".

If the call raises celery.exceptions.RetryTaskError, it extracts the original exception, uses that as the result and sets the task status to "RETRY".

If the call results in an exception, it saves the exception as the task result, and sets the task status to "FAILURE".

Parameters:
  • fun – Callable object to execute.
  • task_id – The unique id of the task.
  • task_name – Name of the task.
  • args – List of positional args to pass on to the function.
  • kwargs – Keyword arguments mapping to pass on to the function.
Returns:

the function return value on success, or the exception instance on failure.

handle_failure(exc, exc_info)
Handle exception.
handle_retry(exc, exc_info)
Handle retry exception.
handle_success(retval)

Handle successful execution.

Saves the result to the current result store (skipped if the callable
has a ignore_result attribute set to True).

If the callable has a on_success function, it as called with retval as argument.

Parameter:retval – The return value.
celery.execute.apply(task, args, kwargs, **options)

Apply the task locally.

This will block until the task completes, and returns a celery.result.EagerResult instance.

celery.execute.apply_async(task, args=None, kwargs=None, countdown=None, eta=None, routing_key=None, exchange=None, task_id=None, immediate=None, mandatory=None, priority=None, connection=None, connect_timeout=4, **opts)

Run a task asynchronously by the celery daemon(s).

Parameters:
  • task – The task to run (a callable object, or a Task instance
  • args – The positional arguments to pass on to the task (a list).
  • kwargs – The keyword arguments to pass on to the task (a dict)
  • countdown – Number of seconds into the future that the task should execute. Defaults to immediate delivery (Do not confuse that with the immediate setting, they are unrelated).
  • eta – A datetime.datetime object that describes the absolute time when the task should execute. May not be specified if countdown is also supplied. (Do not confuse this with the immediate setting, they are unrelated).
  • routing_key – The routing key used to route the task to a worker server.
  • exchange – The named exchange to send the task to. Defaults to celery.task.base.Task.exchange.
  • immediate – Request immediate delivery. Will raise an exception if the task cannot be routed to a worker immediately. (Do not confuse this parameter with the countdown and eta settings, as they are unrelated).
  • mandatory – Mandatory routing. Raises an exception if there’s no running workers able to take on this task.
  • connection – Re-use existing AMQP connection. The connect_timeout argument is not respected if this is set.
  • connect_timeout – The timeout in seconds, before we give up on establishing a connection to the AMQP server.
  • priority – The task priority, a number between 0 and 9.
celery.execute.delay_task(task_name, *args, **kwargs)

Delay a task for execution by the celery daemon.

Parameters:
  • task_name – the name of a task registered in the task registry.
  • *args – positional arguments to pass on to the task.
  • **kwargs – keyword arguments to pass on to the task.
Raises celery.exceptions.NotRegistered:
 

exception if no such task has been registered in the task registry.

Return type:

celery.result.AsyncResult.

Example

>>> r = delay_task("update_record", name="George Constanza", age=32)
>>> r.ready()
True
>>> r.result
"Record was updated"

Previous topic

Defining Tasks - celery.task.base

Next topic

Task Result - celery.result

This Page