celery by dummy

12
By Os Shiowattana (@djshiow)

Upload: dungjit-shiowattana

Post on 16-Jul-2015

94 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Celery by dummy

By

Os Shiowattana (@djshiow)

Page 2: Celery by dummy

Who am I

● Os Shiowattana (@djshiow)● Software engineer for ~10 years● Currently at Origami Logic

○ We’re looking for great developers ;-)● Python experience: 3 months

○ Teaching is a good way to learn

Page 3: Celery by dummy

Outline

● What is Celery?● What is it for?● Simple Example

http://www.myhdiet.com/sharons-raw-celery-soup/

Page 4: Celery by dummy

What is celery?

● Task queue○ Distributed○ Asynchronous

● Create tasks and add it to the queue

● Workers pick them up & work on them

Page 5: Celery by dummy

How does it work?

Broker

Worker

Worker

Worker

….

Producer

ResultStore

Page 6: Celery by dummy

What is it for?

● Long running asynchronous task● Execute a task “reliably”

○ Asynchronous + Retries○ e.g. Interactions with external APIs

● Scheduling Periodic tasks

Page 7: Celery by dummy

How does Origami Logic use it?

● Fetch marketing metrics from all your social network daily○ Periodic○ Reliable (with retries) / Traceable○ Distributed (multiple machines) & Scalable○ Rate limits

Page 8: Celery by dummy

Instagram

● Populate follower feed

>7 M followers

Source: http://blogs.vmware.com/vfabric/2013/04/how-instagram-feeds-work-celery-and-rabbitmq.html

Page 9: Celery by dummy

Simple Example

● Adding two numbers asynchronously !!● Setup:

○ Install RabbitMQ○ Install Celery

From: http://celery.readthedocs.org/en/latest/getting-started/first-steps-with-celery.html

Page 10: Celery by dummy

First, define Celery app & tasks from celery import Celery

#setup app

app = Celery('tasks', backend='amqp' , broker='amqp://guest@localhost//')

#Define task

@app.task

def add(x, y):

return x + y

Then, run celery worker

celery -A tasks worker --loglevel=info

Decorator to create a task class

Page 11: Celery by dummy

Lastly, trigger the taskfrom tasks import add

#run the add task asynchronously 3 seconds later

#passing (4,4) as paramadd.apply_async((4, 4) , countdown=3)

[2015-03-15 14:34:10,397: INFO/MainProcess] Received task: tasks.add[b68b0109-7e99-473b-8991-1da8adaa50e8] eta:[2015-03-15 14:34:13.385448+00:00]

[2015-03-15 14:34:14,836: INFO/MainProcess] Task tasks.add[b68b0109-7e99-473b-8991-1da8adaa50e8] succeeded in 0.0209855769999s: 8