Use async() from your code to quickly offload tasks to the Cluster:

from django_q import async, result

# create the task
async('math.copysign', 2, -2)

# or with import and storing the id
import math.copysign

task_id = async(copysign, 2, -2)

# get the result
task_result = result(task_id)

# result returns None if the task has not been executed yet
# so in most cases you will want to use a hook:

async('math.modf', 2.5, hook='hooks.print_result')

# hooks.py
def print_result(task):

Synchronous testing

async() can be instructed to execute a task immediately by setting the optional keyword sync=True. The task will then be injected straight into a worker and the result saved by a monitor instance:

from django_q import async, fetch

# create a synchronous task
task_id = async('my.buggy.code', sync=True)

# the task will then be available immediately
task = fetch(task_id)

# and can be examined
if not task.success:
    print('An error occurred: {}'.format(task.result))
An error occurred: ImportError("No module named 'my'",)

Note that async() will block until the task is executed and saved. This feature bypasses the Redis server and is intended for debugging and development.

Connection pooling

Django Q tries to pass redis connections around its parts as much as possible to save you from running out of connections. When you are making individual calls to async() a lot though, it can help to set up a redis connection to pass to async():

# redis connection economy example
from django_q import async
from django_q.conf import redis_client

for i in range(50):
    async('math.modf', 2.5, redis=redis_client)


If you are using django-redis , you can configure Django Q to use its connection pool.


async(func, *args, hook=None, timeout=None, sync=False, redis=None, **kwargs)
Puts a task in the cluster queue
  • func (object) – The task function to execute
  • args – The arguments for the task function
  • hook (object) – Optional function to call after execution
  • timeout (int) – Overrides global cluster timeout.
  • sync (bool) – If set to True, async will simulate a task execution
  • redis – Optional redis connection
  • kwargs – Keyword arguments for the task function

The uuid of the task

Return type:



Gets the result of a previously executed task

Parameters:task_id (str) – the uuid or name of the task
Returns:The result of the executed task

Returns a previously executed task

Parameters:name (str) – the uuid or name of the task
Returns:The task
Return type:Task

Changed in version 0.2.0.

Renamed from get_task

class Task

Database model describing an executed task


An uuid.uuid4() identifier


The name of the task as a humanized version of the id


This is for convenience and can be used as a parameter for most functions that take a task_id. Keep in mind however that it is not guaranteed to be unique if you store very large amounts of tasks in the database.


The function or reference that was executed


The function to call after execution.


Positional arguments for the function.


Keyword arguments for the function.


The result object. Contains the error if any occur.


The moment the task was created by an async command


The moment a worker finished this task


Was the task executed without problems?


Calculates the difference in seconds between started and stopped.


Time taken represents the time a task spends in the cluster, this includes any time it may have waited in the queue.

classmethod get_result(task_id)

Get a result directly by task uuid or name

class Success

A proxy model of Task with the queryset filtered on Task.success is True.

class Failure

A proxy model of Task with the queryset filtered on Task.success is False.