workers.rst 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. Data structures
  10. ===============
  11. ready_queue
  12. -----------
  13. The ready queue is either an instance of :class:`Queue.Queue`, or
  14. `celery.buckets.TaskBucket`. The latter if rate limiting is enabled.
  15. eta_schedule
  16. ------------
  17. The ETA schedule is a heap queue sorted by time.
  18. Components
  19. ==========
  20. CarrotListener
  21. --------------
  22. Receives messages from the broker using ``carrot``.
  23. When a message is received it's converted into a
  24. :class:`celery.worker.job.TaskWrapper` object.
  25. Tasks with an ETA are entered into the ``eta_schedule``, messages that can
  26. be immediately processed are moved directly to the ``ready_queue``.
  27. ScheduleController
  28. ------------------
  29. The schedule controller is running the ``eta_schedule``.
  30. If the scheduled tasks eta has passed it is moved to the ``ready_queue``,
  31. otherwise the thread sleeps until the eta is met (remember that the schedule
  32. is sorted by time).
  33. Mediator
  34. --------
  35. The mediator simply moves tasks in the ``ready_queue`` over to the
  36. task pool for execution using
  37. :meth:`celery.worker.job.TaskWrapper.execute_using_pool`.
  38. TaskPool
  39. --------
  40. This is a slightly modified :class:`multiprocessing.Pool`.
  41. It mostly works the same way, except it makes sure all of the workers
  42. are running at all times. If a worker is missing, it replaces
  43. it with a new one.