Sobes.tech
Junior — Middle

Describe the process of launching and configuring Celery for task execution in an application.

sobes.tech AI

Answer from AI

To run and configure Celery in an application, you need to follow several steps:

  1. Install Celery: using pip pip install celery.

  2. Configure the message broker: Celery requires a broker, such as RabbitMQ or Redis. Specify the broker URL in the settings.

  3. Create a Celery instance: create a Celery object in the code and configure it.

  4. Define tasks: tasks are defined as functions decorated with @celery.task.

  5. Start the worker: using the command celery -A your_module worker --loglevel=info.

Example of minimal configuration:

from celery import Celery

app = Celery('tasks', broker='redis://localhost:6379/0')

@app.task
def add(x, y):
    return x + y

Then start the worker and call the task:

add.delay(4, 6)

This will send the task to the queue, and the worker will execute it asynchronously.