+ All Categories
Home > Documents > Celery - An A Synchronous Task Queue (Not Only) for Django

Celery - An A Synchronous Task Queue (Not Only) for Django

Date post: 24-Mar-2015
Category:
Upload: markus-zapke-gruendemann
View: 1,745 times
Download: 1 times
Share this document with a friend
Description:
Celery is an asynchronous task queue. It can be used to process large amounts of messages in real-time.
39
Celery An asynchronous task queue (not only) for Django Markus Zapke-Gründemann DjangoCon Europe 2011 Image (cc-by-sa): http://www.flickr.com/photos/artdrauglis/3747272595/
Transcript
Page 1: Celery - An A Synchronous Task Queue (Not Only) for Django

CeleryAn asynchronous task queue

(not only) for DjangoMarkus Zapke-Gründemann

DjangoCon Europe 2011

Image (cc-by-sa): http://www.flickr.com/photos/artdrauglis/3747272595/

Page 2: Celery - An A Synchronous Task Queue (Not Only) for Django

Overview

• Why should I use a task queue?

• Celery

• Python Task

• Django Task

• Webhooks

Page 3: Celery - An A Synchronous Task Queue (Not Only) for Django

Why should I usea task queue?

Page 4: Celery - An A Synchronous Task Queue (Not Only) for Django

Users have to wait…

Action Task

Add a comment Check for spam

Upload an image Create thumbnails

Order a product Process payment,send emails

Mark a favorite Update database

Page 5: Celery - An A Synchronous Task Queue (Not Only) for Django

A task queue can help!

• Decoupling of information producers and consumers

• Profit from asynchronous processing

• Improve scalability

• Replace cronjobs

Page 6: Celery - An A Synchronous Task Queue (Not Only) for Django

Client Broker Worker

Page 7: Celery - An A Synchronous Task Queue (Not Only) for Django

Client Broker

Client

Client

Worker

Worker

Page 8: Celery - An A Synchronous Task Queue (Not Only) for Django

Client Broker

Client

Client

Worker

Worker

1

2

Page 9: Celery - An A Synchronous Task Queue (Not Only) for Django

Client Broker

Client

Client

Worker

Worker

1

2

34

Page 10: Celery - An A Synchronous Task Queue (Not Only) for Django

Client Broker

Client

Client

Worker

Worker

DatabaseFilesystem

API

1

3

2

45

Page 11: Celery - An A Synchronous Task Queue (Not Only) for Django

Celery

Page 12: Celery - An A Synchronous Task Queue (Not Only) for Django

Celery

• Author: Ask Solem Hoel

• Written in Python

• Synchronous, asynchronous and scheduled tasks

• Broker: RabbitMQ, NoSQL and Ghetto Queue

Page 13: Celery - An A Synchronous Task Queue (Not Only) for Django

RabbitMQ

• Message Broker

• Written in Erlang

• AMQP (Advanced Message Queuing Protocol)

• Clustering support

Page 14: Celery - An A Synchronous Task Queue (Not Only) for Django

CeleryFeatures

• Serialization (pickle, JSON, YAML or custom code)

• Task sets and subtasks

• Retry failed tasks

• Routing (only AMQP brokers)

Page 15: Celery - An A Synchronous Task Queue (Not Only) for Django

CeleryFeatures

• Different result stores

• Easy logging from tasks

• Webhooks

• Integration with Python Web Frameworks (Django, Flask, Pylons)

Page 16: Celery - An A Synchronous Task Queue (Not Only) for Django

CeleryHistory

• Until 1.0.6 the Django ORM was used

• Since 2.0 SQLAlchemy is used and Django integration is provided by the „django-celery“ package

Page 17: Celery - An A Synchronous Task Queue (Not Only) for Django

Install Celery

$ pip install celery

Page 18: Celery - An A Synchronous Task Queue (Not Only) for Django

Behind the scenes

• kombu - An AMQP messaging framework for Python

• amqplib - Python AMQP client.

• anyjson - A uniform JSON API

Page 19: Celery - An A Synchronous Task Queue (Not Only) for Django

Setting up RabbitMQ

$ rabbitmqctl add_user myuser mypassword

$ rabbitmqctl add_vhost myvhost

$ rabbitmqctl set_permissions -p myvhost myuser ".*" ".*" ".*"

Page 20: Celery - An A Synchronous Task Queue (Not Only) for Django

Python Task

Page 21: Celery - An A Synchronous Task Queue (Not Only) for Django

Python Task

# tasks.pyfrom celery.task import task

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

Page 22: Celery - An A Synchronous Task Queue (Not Only) for Django

Python TaskConfiguration

# celeryconfig.pyBROKER_HOST = "localhost"BROKER_PORT = 5672BROKER_USER = "myuser"BROKER_PASSWORD = "mypassword"BROKER_VHOST = "myvhost"CELERY_RESULT_BACKEND = "amqp"CELERY_IMPORTS = ("tasks", )

Page 23: Celery - An A Synchronous Task Queue (Not Only) for Django

Python Task Execution>>> from tasks import add>>> result = add.delay(1, 2)>>> result.ready()False

$ celeryd --loglevel=INFO

>>> result.get()3>>> result.result3>>> result.successful()True

Page 24: Celery - An A Synchronous Task Queue (Not Only) for Django

Django Task

Page 25: Celery - An A Synchronous Task Queue (Not Only) for Django

Install Celeryfor Django

$ pip install django-celery

Page 26: Celery - An A Synchronous Task Queue (Not Only) for Django

Client Broker Worker

Django Task

Page 27: Celery - An A Synchronous Task Queue (Not Only) for Django

Client Broker Worker

Django Task

blog.views.add_comment

Page 28: Celery - An A Synchronous Task Queue (Not Only) for Django

Client Broker Worker

Django Task

blog.views.add_comment blog.tasks.spam_filter

Page 29: Celery - An A Synchronous Task Queue (Not Only) for Django

Client Broker Worker

Django Task

blog.views.add_comment blog.tasks.spam_filter

CommentModel

Page 30: Celery - An A Synchronous Task Queue (Not Only) for Django

Client Broker Worker

Django Task

blog.views.add_comment blog.tasks.spam_filter

CommentModel

comment.save()tasks.spam_filter.delay(comment.id, remote_addr)

Page 31: Celery - An A Synchronous Task Queue (Not Only) for Django

Client Broker Worker

Django Task

blog.views.add_comment blog.tasks.spam_filter

CommentModel

comment.save()tasks.spam_filter.delay(comment.id, remote_addr)

comment.is_spam = Truecomment.save()

Page 32: Celery - An A Synchronous Task Queue (Not Only) for Django

Django Task# blog/tasks.py@taskdef spam_filter(comment_id, remote_addr=None): logger = spam_filter.get_logger() logger.info("Running spam filter for comment %s" % comment_id)

comment = Comment.objects.get(pk=comment_id) current_domain = Site.objects.get_current().domain akismet = Akismet(settings.AKISMET_KEY, "http://%s" % current_domain) if not akismet.verify_key(): raise ImproperlyConfigured("Invalid AKISMET_KEY")

is_spam = akismet.comment_check(user_ip=remote_addr, comment_content=comment.comment, comment_author=comment.name, comment_author_email=comment.email_address) if is_spam: comment.is_spam = True comment.save()

return is_spam

Page 33: Celery - An A Synchronous Task Queue (Not Only) for Django

Django Task# blog/tasks.py@taskdef spam_filter(comment_id, remote_addr=None): logger = spam_filter.get_logger() logger.info("Running spam filter for comment %s" % comment_id)

comment = Comment.objects.get(pk=comment_id) current_domain = Site.objects.get_current().domain akismet = Akismet(settings.AKISMET_KEY, "http://%s" % current_domain) if not akismet.verify_key(): raise ImproperlyConfigured("Invalid AKISMET_KEY")

is_spam = akismet.comment_check(user_ip=remote_addr, comment_content=comment.comment, comment_author=comment.name, comment_author_email=comment.email_address) if is_spam: comment.is_spam = True comment.save()

return is_spam

Page 34: Celery - An A Synchronous Task Queue (Not Only) for Django

Django TaskConfiguration

# settings.pyINSTALLED_APPS += ("djcelery", )

import djcelerydjcelery.setup_loader()

BROKER_HOST = "localhost"BROKER_PORT = 5672BROKER_USER = "myuser"BROKER_PASSWORD = "mypassword"BROKER_VHOST = "myvhost"

$ python manage.py syncdb$ python manage.py celeryd -l info

Page 35: Celery - An A Synchronous Task Queue (Not Only) for Django

Webhooks

Page 36: Celery - An A Synchronous Task Queue (Not Only) for Django

Webhooks

# POST>>> from celery.task.http import URL>>> res = URL("http://example.com/multiply").get_async(x=10, y=10)>>> res.get() # {"status": "success", "retval": 100}100

# GET>>> from celery.task.http import HttpDispatchTask>>> url = "http://example.com/multiply">>> res = HttpDispatchTask.delay(url, method="GET", x=10, y=10)>>> res.get() # {"status": "success", "retval": 100}100

Page 37: Celery - An A Synchronous Task Queue (Not Only) for Django

Links

• http://celeryproject.org/

• http://www.rabbitmq.com/

• http://github.com/ask

• http://pypi.python.org/pypi/celery

• http://pypi.python.org/pypi/django-celery

• http://pypi.python.org/pypi/celerymon

Page 38: Celery - An A Synchronous Task Queue (Not Only) for Django

Thanks!@keimlink

http://www.keimlink.de

Page 39: Celery - An A Synchronous Task Queue (Not Only) for Django

License

This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/

licenses/by-sa/3.0/ or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California,

94041, USA.


Recommended