← Back to Libraries🤖 Automation
📦

celery in Python

Learn how to use the celery library in Python with practical examples and best practices.

pip install celery

Overview

Paragraph 1: Introduction to celery and what it does

Paragraph 2: Key features and common use cases

Paragraph 3: Installation and getting started

Code Examples

Basic Example

from celery import Celery

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

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

# To call the task asynchronously:
# result = add.delay(4, 6)
# print(result.get())

Common Methods

Celery.task

Decorator to register a function as a Celery task, making it possible to execute the function asynchronously.

task.delay(*args, **kwargs)

Call the task asynchronously with the given arguments. Returns an AsyncResult instance.

AsyncResult.get(timeout=None)

Waits for the task to finish and returns the result. Can accept a timeout.

Celery.send_task(name, args=None, kwargs=None, **options)

Send a task by name, useful if you don't have direct access to the decorated function.

Celery.conf

Access and modify the configuration of your Celery application instance.

More Automation Libraries