python celery - pycon india · celery commands are part of django commands run celery workers using...

21
 Python Celery The Distributed Task Queue Mahendra M @mahendra http://creativecommons.org/licenses/by-sa/3.0/

Upload: others

Post on 23-Aug-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Python Celery - PyCon India · Celery commands are part of django commands Run celery workers using manage.py Task registeration and auto discovery tasks.py Schedule jobs directly

   

Python Celery

The Distributed Task Queue

Mahendra M

@mahendra

http://creativecommons.org/licenses/by-sa/3.0/

Page 2: Python Celery - PyCon India · Celery commands are part of django commands Run celery workers using manage.py Task registeration and auto discovery tasks.py Schedule jobs directly

   

About me

● Solutions Architect at Infosys, Product Incubation Group

● Worked on FOSS for 10 years● BLUG, FOSS.in (ex) member● Linux, NetBSD embedded developer● Mostly Python programmer

● Mostly sticks to server side stuff

● Loves CouchDB and Twisted

Page 3: Python Celery - PyCon India · Celery commands are part of django commands Run celery workers using manage.py Task registeration and auto discovery tasks.py Schedule jobs directly

   

Job Queues – the need

● More complex systems on the web● Asynchronous processing is required

● Flickr – Image resizing● User profiles – synchronization

● Asynchronous database updates● Click counters● Likes, favourites, recommend

Page 4: Python Celery - PyCon India · Celery commands are part of django commands Run celery workers using manage.py Task registeration and auto discovery tasks.py Schedule jobs directly

   

How it works

User Web service Job Queue

Worker

Worker

12

3

Placing a request

Page 5: Python Celery - PyCon India · Celery commands are part of django commands Run celery workers using manage.py Task registeration and auto discovery tasks.py Schedule jobs directly

   

How it works

User Web service Job Queue

Worker

Worker

2

The job is executed

4

5

6

Page 6: Python Celery - PyCon India · Celery commands are part of django commands Run celery workers using manage.py Task registeration and auto discovery tasks.py Schedule jobs directly

   

How it works

User Web service Job Queue

Worker

Worker

8

2

7

Client fetches the result

4

5

6

Page 7: Python Celery - PyCon India · Celery commands are part of django commands Run celery workers using manage.py Task registeration and auto discovery tasks.py Schedule jobs directly

   

AMQP

● Advanced Message Queuing Protocol● Self explanatory :­)

● Open, language agnostic, implementation agnostic● Transmits messages from producers to consumers● Immensely popular● Open source implementations available.

Page 8: Python Celery - PyCon India · Celery commands are part of django commands Run celery workers using manage.py Task registeration and auto discovery tasks.py Schedule jobs directly

   

AMQP

Page 9: Python Celery - PyCon India · Celery commands are part of django commands Run celery workers using manage.py Task registeration and auto discovery tasks.py Schedule jobs directly

   

AMQP

Queues are bound to exchanges to determine message delivery

● Direct ­ From Exchange to Queue● Topic ­ Queue is selected based on a topic● Fanout – All queues are selected● Headers – based on message headers

Page 10: Python Celery - PyCon India · Celery commands are part of django commands Run celery workers using manage.py Task registeration and auto discovery tasks.py Schedule jobs directly

   

AMQP as a job queue

● AMQP structure is similar to our job queue design● Jobs are sent as messages● Job results are sent back as messages● Celery Framework simplifies this for us

Page 11: Python Celery - PyCon India · Celery commands are part of django commands Run celery workers using manage.py Task registeration and auto discovery tasks.py Schedule jobs directly

   

Python Celery

● Python based distributed task queue built on top of message queues

● Very robust, good error handling, guaranteed ...● Distributed (across machines)● Concurrent within a box● Supports job scheduling (eta, cron, date, …)● Synchronous and asynchronous operations● Retries and task grouping

Page 12: Python Celery - PyCon India · Celery commands are part of django commands Run celery workers using manage.py Task registeration and auto discovery tasks.py Schedule jobs directly

   

Python Celery ...

● Web hooks● Job Routing 

● based on AMQP message routing

● Remote control of workers● Rate limit, delete, revoke tasks

● Monitoring● Tracebacks of errors, Email notifications

● Django Integration

Page 13: Python Celery - PyCon India · Celery commands are part of django commands Run celery workers using manage.py Task registeration and auto discovery tasks.py Schedule jobs directly

   

Celery Architecture

Page 14: Python Celery - PyCon India · Celery commands are part of django commands Run celery workers using manage.py Task registeration and auto discovery tasks.py Schedule jobs directly

   

Defining a task

from celery.decorators import task

@taskdef add(x, y): return x + y

>>> result = add.delay(4, 4)>>> result.wait() # wait for result8>>>

Page 15: Python Celery - PyCon India · Celery commands are part of django commands Run celery workers using manage.py Task registeration and auto discovery tasks.py Schedule jobs directly

   

Running a task

● Synchronous run● task.apply( … )

● Asynchrnous● task.apply_async( … )

● Tasksets – schedule task with different arguments● Think of it like map reduce

● Scheduled execution● Auto retries and max_retry support● Ensure worker availability

Page 16: Python Celery - PyCon India · Celery commands are part of django commands Run celery workers using manage.py Task registeration and auto discovery tasks.py Schedule jobs directly

   

Django Features

● 'djcelery' in INSTALLED_APPS● Uses Django features

● ORM for storing task details● settings.py for configuration● Celery commands are part of django commands● Run celery workers using manage.py● Task registeration and auto discovery ­ tasks.py

● Schedule jobs directly from view handlers● View handlers for task status monitoring via Ajax

Page 17: Python Celery - PyCon India · Celery commands are part of django commands Run celery workers using manage.py Task registeration and auto discovery tasks.py Schedule jobs directly

   

Demo

Page 18: Python Celery - PyCon India · Celery commands are part of django commands Run celery workers using manage.py Task registeration and auto discovery tasks.py Schedule jobs directly

   

Advantages of AMQP

● Scaling is an ”admin” job● Workers can be added and removed any time● Scale on need basis● Deploy on cloud setups

● Jobs can routed to workers based on admin setups● Jobs can be prioritized based on AMQP protocol

● Not supported in rabbitmq (not sure of 2.x)

● Can be deployed on a single node also.

Page 19: Python Celery - PyCon India · Celery commands are part of django commands Run celery workers using manage.py Task registeration and auto discovery tasks.py Schedule jobs directly

   

Where should I use

● Background computations● Anything outside the request­response cycle● Run System commands or applications

● Imagemagick (convert) for resize

● Integration with external systems (APIs)● Use webhooks for Integrating independent systems● Result aggregations (db updates, like, ratings ..)

Page 20: Python Celery - PyCon India · Celery commands are part of django commands Run celery workers using manage.py Task registeration and auto discovery tasks.py Schedule jobs directly

   

Where to avoid ?

● Ensure that you absolutely need a task queue● Sometimes it might be easier to avoid it● Simple database updates / inserts (log like)● Sending emails/sms (it is already a message 

queue ...)

Page 21: Python Celery - PyCon India · Celery commands are part of django commands Run celery workers using manage.py Task registeration and auto discovery tasks.py Schedule jobs directly

   

Links

● http://celeryproject.org● http://celery.org/docs/getting­started/● http://amqp.org/● http://en.wikipedia.org/AMQP● http://rabbitmq.org/● http://slideshare.net/search/slideshow?q=celery