How to create a task with Celery

In this recipe, we'll show you how to create and call a task using the Celery module. Celery provides the following methods that make a call to a task:

  • apply_async(args[, kwargs[, …]]): This task sends a task message
  • delay(*args, **kwargs): This is a shortcut to send a task message, but does not support execution options

The delay method is better to use because it can be called as a regular function:

task.delay(arg1, arg2, kwarg1='x', kwarg2='y')

While using apply_async you should write:

task.apply_async (args=[arg1, arg2] kwargs={'kwarg1': 'x','kwarg2': 'y'})

How to do it…

To perform this simple task, we implement the following two simple scripts:

### ## addTask.py :Executing a simple task ### from celery import Celery ...

Get Python Parallel Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.