Skip to content

Chains

Sometimes you want to run tasks sequentially. For that you can use the async_chain function:

# async a chain of tasks
from django_q.tasks import async_chain, result_group

# the chain must be in the format
# [(func,(args),{kwargs}),(func,(args),{kwargs}),..]
group_id = async_chain([('math.copysign', (1, -1)),
                          ('math.floor', (1,))])

# get group result
result_group(group_id, count=2)

A slightly more convenient way is to use a Chain instance:

# Chain async
from django_q.tasks import Chain

# create a chain that uses the cache backend
chain = Chain(cached=True)

# add some tasks
chain.append('math.copysign', 1, -1)
chain.append('math.floor', 1)

# run it
chain.run()

print(chain.result())
[-1.0, 1]

Reference

async_chain()

async_chain(chain, group=None, cached=Conf.CACHED, sync=Conf.SYNC, broker=None)

Async a chain of tasks. See also the Chain class.

Parameter Type Description
chain list a list of tasks in the format [(func,(args),{kwargs}), (func,(args),{kwargs})]
group str an optional group name.
cached bool run this against the cache backend
sync bool execute this inline instead of asynchronous
broker an optional broker instance

Chain

Chain(chain=None, group=None, cached=Conf.CACHED, sync=Conf.SYNC, broker=None)

A sequential chain of tasks. Acts as a convenient wrapper for async_chain You can pass the task chain at construction or you can append individual tasks before running them.

Parameter Type Description
chain list a list of task in the format [(func,(args),{kwargs}), (func,(args),{kwargs})]
group str an optional group name.
cached bool run this against the cache backend
sync bool execute this inline instead of asynchronous
broker an optional broker instance

Methods

append()

append(func, *args, **kwargs)

Append a task to the chain. Takes the same arguments as async_task

Returns: the current number of tasks in the chain (int).

run()

run()

Start queueing the chain to the worker cluster.

Returns: the chains group id

result()

result(wait=0)

return the full list of results from the chain when it finishes. Blocks until timeout or result.

Parameter Type Description
wait int how many milliseconds to wait for a result

Returns: an unsorted list of results

fetch()

fetch(failures=True, wait=0)

get the task result objects from the chain when it finishes. Blocks until timeout or result.

Parameter Type Description
failures include failed tasks
wait int how many milliseconds to wait for a result

Returns: an unsorted list of task objects

current()

current()

get the index of the currently executing chain element

Returns: current chain index (int).

length()

length()

get the length of the chain

Returns: length of the chain (int).