Introduction to Django-Celery and Supervisor

28
Introductio n to Celery

Transcript of Introduction to Django-Celery and Supervisor

Page 1: Introduction to Django-Celery and Supervisor

Introduction to

Celery

Page 2: Introduction to Django-Celery and Supervisor

What is Celery?

“Celery is an asynchronous task queue based on distributed message passing.”It is a powerful, production-ready asynchronous job queue, which allows you to run time-consuming Python functions in the background.

Page 3: Introduction to Django-Celery and Supervisor

Why should I use Celery?

Page 4: Introduction to Django-Celery and Supervisor

User perspective• Minimize request/response cycle• Smoother user experience• Difference between pleasant and

unpleasant

Page 5: Introduction to Django-Celery and Supervisor

Developer perspective

• Offload time/cpu intensive processes• Scalability - add workers as needed• Flexibility - many points of customization• Actively developed• Great documentation• Lots of tutorials

Page 6: Introduction to Django-Celery and Supervisor

OUT OF THE REQUEST/RESPONSE

CYCLE• Example: Sending emails

asynchronously.

Page 7: Introduction to Django-Celery and Supervisor

TASKS IN THEBACKGROUND.

• Example: Computational heavy jobs.• Example: Interacting with external

APIs.

Page 8: Introduction to Django-Celery and Supervisor

PERIODIC JOBS

Page 9: Introduction to Django-Celery and Supervisor
Page 10: Introduction to Django-Celery and Supervisor

Celery Architecture

Page 11: Introduction to Django-Celery and Supervisor

PRODUCER• Produces a task for the queue.

Page 12: Introduction to Django-Celery and Supervisor

BROKER• Stores the task backlog• Answers, what work remains to be

done?• RabbitMQ, Redis, SQLAlchemy,

Django's ORM, MongoDB

Page 13: Introduction to Django-Celery and Supervisor

WORKER• Execute and consumes tasks.• Distributed.

Page 14: Introduction to Django-Celery and Supervisor

RESULTS BACKEND• Stores the results from our tasks.

Page 15: Introduction to Django-Celery and Supervisor

EXAMPLE

Page 16: Introduction to Django-Celery and Supervisor

INTERGRATING WITHDJANGO.

Page 17: Introduction to Django-Celery and Supervisor

BEWARE OF DJANGO-CELERY

Page 18: Introduction to Django-Celery and Supervisor

IMPORTANT SETTINGS• BROKER_URL = 'redis://localhost:6379/0'• CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'• CELERY_TASK_RESULT_EXPIRES = 7*86400 # 7 days• CELERYBEAT_SCHEDULER =

"djcelery.schedulers.DatabaseScheduler"• CELERY_ACCEPT_CONTENT = ['application/json']• CELERY_TASK_SERIALIZER = 'json'• CELERY_RESULT_SERIALIZER = 'json'• BROKER_BACKEND = "redis"• BROKER_CONNECTION_TIMEOUT = 5.0• CELERY_IMPORTS = ('<your-app-name>.tasks',)

Page 19: Introduction to Django-Celery and Supervisor

Write code in celery.py

Page 20: Introduction to Django-Celery and Supervisor

Intialize celery in __init__.py

Page 21: Introduction to Django-Celery and Supervisor

BEST PRACTICES

Page 22: Introduction to Django-Celery and Supervisor

NEVER PASS OBJECTS ASARGUMENTS.

Page 23: Introduction to Django-Celery and Supervisor

AVOID LAUNCHING SYNCHRONOUS SUBTASKS

Page 24: Introduction to Django-Celery and Supervisor
Page 25: Introduction to Django-Celery and Supervisor

PERIODIC TASKS

Page 26: Introduction to Django-Celery and Supervisor

THINGS GO WRONG ?

Page 27: Introduction to Django-Celery and Supervisor

RE-TRY!

Page 28: Introduction to Django-Celery and Supervisor

Thanks!