O'Reilly logo

Python Parallel Programming Cookbook by Giancarlo Zaccone

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required