celery in the django

12
叢叢叢叢叢叢 Celery in the Django By Walter Liu

Upload: walter-liu

Post on 15-Jan-2015

2.048 views

Category:

Documents


4 download

DESCRIPTION

Shortly introduce Celery and some pitfalls of using Celery.

TRANSCRIPT

Page 1: Celery in the Django

叢林裡的小芹菜Celery in the Django

By Walter Liu

Page 2: Celery in the Django

About me Learn Python since 2004 Was a Chief Technical Director in

Softstar. Now an Architect in Trend Micro Light Celery user for several months.

Page 3: Celery in the Django

Celery is XDistributed

AsynchronousTask Queue

Page 4: Celery in the Django

Have you met? Deliver thousands to millions e-mails to

your members? Many computation heavy queries at the

same time that blocks your web site or DB?

Query slow or too many connection to external API? (like Twitter/Facebook)

Long run tasks that occupied your web server resources?

Page 5: Celery in the Django
Page 6: Celery in the Django

Why Celery? POC without Celery

Add a “@celery.task” and the tasks become distributed tasks.

Multi-queue, routing, all configurable. Many utility functionality to help task

execution Group/Chord/Chain/Callback Retry/timeout Etc…..

Page 7: Celery in the Django

from celery import Celery @celery.task( max_retries = 3, default_retry_delay=2 * 60, # retry in 2 minutes. rate_limit = 20, time_limit = 10, # hard time limit soft_time_limit = 5, ignore_result = True, acks_late = True, # make sure this task will be done. )def send_twitter_status(oauth, tweet): try: twitter = Twitter(oauth) twitter.update_status(tweet) except (Twitter.FailWhaleError, Twitter.LoginError), exc: raise send_twitter_status.retry(exc=exc)

Page 8: Celery in the Django

Understand the personality of your celery What happen if the program throw an

exception? What happen if the program hard/soft

time limit? What happen if the hard/soft time limit

reached in your group/chord function? Many others.

Page 9: Celery in the Django

Our pitfall/trouble cases Many, too many small tasks

For example 1 billion per day. Large task argument (workaround => store task at

cache) Send file directly

Better not wait another task in a task. Using Redis as AMQP and no HA. Hard time limit reached in Chord function. Multi-process and multi-thread problem in Windows Logging. Queue management

Page 10: Celery in the Django

Some internet complain about Celery Only Python API change and break his program

2.5.x => 3.0.x Pylint

Page 11: Celery in the Django

Some similar things Gearman Huey Advanced Python Scheduler

Page 12: Celery in the Django

Recruiting – we’re using Front-end: not very limited. Backend

Python Django MongoDB Redis/Memcache Celery