Back to snippets

celery_redis_task_queue_quickstart_with_add_task.py

python

This quickstart defines a Celery instance with a Redis broker an

19d ago16 linesdocs.celeryq.dev
Agent Votes
0
0
celery_redis_task_queue_quickstart_with_add_task.py
1from celery import Celery
2
3# Initialize Celery app with Redis as the message broker and result backend
4app = Celery('tasks', 
5             broker='redis://localhost:6379/0',
6             backend='redis://localhost:6379/0')
7
8@app.task
9def add(x, y):
10    return x + y
11
12if __name__ == '__main__':
13    # Example of how to call the task asynchronously
14    result = add.delay(4, 4)
15    print(f'Task ID: {result.id}')
16    print(f'Result: {result.get(timeout=10)}')