worker.rst 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 consumer, 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. Data structures
  13. ===============
  14. ready_queue
  15. -----------
  16. The ready queue is either an instance of :class:`Queue.Queue`, or
  17. :class:`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. Consumer
  24. --------
  25. Receives messages from the broker using `Kombu`_.
  26. .. _`Kombu`: http://pypi.python.org/pypi/kombu
  27. When a message is received it's converted into a
  28. :class:`celery.worker.job.TaskRequest` object.
  29. Tasks with an ETA are entered into the `eta_schedule`, messages that can
  30. be immediately processed are moved directly to the `ready_queue`.
  31. ScheduleController
  32. ------------------
  33. The schedule controller is running the `eta_schedule`.
  34. If the scheduled tasks eta has passed it is moved to the `ready_queue`,
  35. otherwise the thread sleeps until the eta is met (remember that the schedule
  36. is sorted by time).
  37. Mediator
  38. --------
  39. The mediator simply moves tasks in the `ready_queue` over to the
  40. task pool for execution using
  41. :meth:`celery.worker.job.TaskRequest.execute_using_pool`.
  42. TaskPool
  43. --------
  44. This is a slightly modified :class:`multiprocessing.Pool`.
  45. It mostly works the same way, except it makes sure all of the workers
  46. are running at all times. If a worker is missing, it replaces
  47. it with a new one.