worker.rst 1.7 KB

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