KEMBAR78
Introduction to Celery | PDF
Celery
The Task Queue
Whatiscelery?
● Celery is a vegetable
● Also used as a spice
Well,NotthatCelery
Celery what we want is a task queue
WhattheFishisaTaskQueue?
Tasks are handled asynchronously either because they are not
initiated by an HTTP request or because they are long-
running jobs that would dramatically reduce the performance
of an HTTP response.
For example, a web application could poll the Some API every
10 minutes to collect the names of the top 100 customers in
a particular service.
A task queue would handle invoking code to call the API,
process the results and store them in a persistent database
for later use.
Again,WhatisCelerythen?
Celery is an asynchronous task queue based on distributed
message passing.
Distributed?
SomeCode…Yay!
from celery import Celery
def make_celery(app):
celery = Celery(app.import_name, broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)
TaskBase = celery.Task
class ContextTask(TaskBase):
abstract = True
def __call__(self, *args, **kwargs):
with app.app_context():
return TaskBase.__call__(self, *args, **kwargs)
celery.Task = ContextTask
return celery
Morecode.
from flask import Flask
app = Flask(__name__)
app.config.update(
CELERY_BROKER_URL='redis://localhost:6379',
CELERY_RESULT_BACKEND='redis://localhost:6379'
)
celery = make_celery(app)
@celery.task()
def add_together(a, b):
return a + b
RunForestRun..
result = add_together.delay(23, 42)
result.wait()
SendEmailsAsynchronously?IfeelYouBro.
Demo.
FewThingstoPonder.
● Start Simple (1 Worker, 1 Queue)
● Monitor it. (Celery Flower)
● Consume tasks faster than you produce them (Shamal)
● Tweaking for Concurrency
● Keep your tasks clean
● Manage DB transactions
AnyQuestions?
Get your lazy ass up and Google it my friend!
ThankYou.

Introduction to Celery