Application¶
The first thing you need is a Celery instance, this is called the celery application or just app in short. Since this instance is used as the entry-point for everything you want to do in Celery, like creating tasks and managing workers, it must be possible for other modules to import it.
In this tutorial you will keep everything contained in a single module.
Creating Task¶
Let’s create the file tasks.py:
from celery import Celery
celery = Celery(‘tasks’, broker=’amqp://guest@localhost//’)
@celery.task def add(x, y):
return x + y
The first argument to Celery is the name of the current module, this is needed so that names can be automatically generated, the second argument is the broker keyword argument which specifies the URL of the message broker you want to use, using RabbitMQ here, which is already the default option.
You defined a single task, called add, which returns the sum of two numbers.