worker.rst 1.7 KB

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