worker.rst 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. =======================
  2. Internals: The worker
  3. =======================
  4. **NOTE** This describes the internals of the development version, not the
  5. current release.
  6. The worker consists of 4 main components: the broker listener, the scheduler,
  7. the mediator and the task pool. All these components runs in parallel working
  8. with two data structures: the ready queue and the ETA schedule.
  9. .. image:: http://cloud.github.com/downloads/ask/celery/Celery1.0-inside-worker.jpg
  10. Data structures
  11. ===============
  12. ready_queue
  13. -----------
  14. The ready queue is either an instance of :class:`Queue.Queue`, or
  15. `celery.buckets.TaskBucket`. The latter if rate limiting is enabled.
  16. eta_schedule
  17. ------------
  18. The ETA schedule is a heap queue sorted by time.
  19. Components
  20. ==========
  21. CarrotListener
  22. --------------
  23. Receives messages from the broker using ``carrot``.
  24. When a message is received it's converted into a
  25. :class:`celery.worker.job.TaskWrapper` object.
  26. Tasks with an ETA are entered into the ``eta_schedule``, messages that can
  27. be immediately processed are moved directly to the ``ready_queue``.
  28. ScheduleController
  29. ------------------
  30. The schedule controller is running the ``eta_schedule``.
  31. If the scheduled tasks eta has passed it is moved to the ``ready_queue``,
  32. otherwise the thread sleeps until the eta is met (remember that the schedule
  33. is sorted by time).
  34. Mediator
  35. --------
  36. The mediator simply moves tasks in the ``ready_queue`` over to the
  37. task pool for execution using
  38. :meth:`celery.worker.job.TaskWrapper.execute_using_pool`.
  39. TaskPool
  40. --------
  41. This is a slightly modified :class:`multiprocessing.Pool`.
  42. It mostly works the same way, except it makes sure all of the workers
  43. are running at all times. If a worker is missing, it replaces
  44. it with a new one.