distributed_task will check in every installed app (INSTALLED_APPS) for a tasks.py file.
Create a tasks.py file in your desired app of choice:
from distributed_task import register_task
@register_task
def my_heavy_task_method():
pass
The decorator adds a delay method to your task. You can decide in runtime if you’d like to execute the task delayed or immediately.
Execute delayed in a worker process:
my_heavy_task_method.delay(*args, **kwargs)
Default method execution (bypasses task distribution):
my_heavy_task_method(*args, **kwargs)
You can pass all args/kwargs to the my_heavy_task_method.delay method as you would call it normally. The serializer is also able to handle Django model instances but not QuerySets.
This works fine:
instance = User.objects.first()
my_heavy_task_method.delay('arg 1', user=instance, some_other_arg=False, some_float=12.5212)
Method return values are not available. Maybe in a further version.