About A father, a husband and a software engineer Passionate in distributed systems, real-time data processing, search engine Work @sentifi as a backend engineer Follow me @duydo 3
What is Celery? Distributed Task Queue Simple, fast, flexible, highly available, scalable Mature, feature rich Open source, BSD License Large community 4
Task Task is a unit of work, building blocks in Celery apps Exists until it has been acknowledged Result of the tasks can be stored or ignored States: PENDING, STARTED, SUCCESS, FAILURE, RETRY, REVOKED Periodic task (cron jobs) 8
Calling Task Options eta a specific date time that is the earliest time at which task will be executed countdown set eta by seconds into the future expires set task’s expire time serializer pickle (default), json, yaml and msgpack compression compress the messages using gzip or bzip2 queue route the tasks to different queues 11
Task Result result.ready() true if the task has been executed result.successful() true if the task executed successfully result.result the return value of the task or exception result.get() blocks until the task is complete, return result or exception 12
Signatures signature() wraps args, kwargs, options of a single task invocation in a way such that it can be: • passed to functions • serialized and sent across the wire like subtasks 14
Chord 21 Like a group but with a callback c = chord((add.s(i, i) for i in xrange(5)), xsum.s()) c = chord(add.s(i, i) for i in xrange(5)) (xsum.s()) c().get() => 20
Chunks 24 Chunking splits a long list of args to parts items = zip(xrange(10), xrange(10)) c = add.chunks(items, 5) c() => [0, 2, 4, 6, 8], [10, 12, 14, 16, 18]
Autoscaling Dynamically resizing the worker pool depending on load or custom metrics defined by user celery worker —autoscale=8,2 => min processes: 2, max processes:8 27
Resource Leak Protection Limit number of tasks a pool worker process can execute before it’s replaced by a new one celery worker —maxtaskperchild=10 29
User Components Celery uses a dependency graph enabling fire grained control of the workers internally, called “bootsteps” Customize the worker components, e.g: ConsumerStep Add new components Bootsteps http://celery.readthedocs.org/en/latest/ userguide/extending.html 31
Monitoring Flower - Real-time Celery web monitor • Task progress and history • Show task details (arguments, start time, runtime, and more) • Graphs and statistics • Shutdown, restart worker instances • Control worker pool size, autoscaling settings • … 32