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):
print(task.result)
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.
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)
Tip
If you are using django-redis , you can configure <django_redis>
Django Q to use its connection pool.
- param func
The task function to execute
- param args
The arguments for the task function
- type func
str or object
- param hook
Optional function to call after execution
- type hook
str or object
- param bool sync
If set to True, async will simulate a task execution
- param redis
Optional redis connection
- param kwargs
Keyword arguments for the task function
- returns
The uuid of the task
- rtype
str