| 1 |
.. _internals-worker: |
| 2 |
|
| 3 |
======================= |
| 4 |
Internals: The worker |
| 5 |
======================= |
| 6 |
|
| 7 |
.. contents:: |
| 8 |
:local: |
| 9 |
|
| 10 |
Introduction |
| 11 |
============ |
| 12 |
|
| 13 |
The worker consists of 4 main components: the consumer, the scheduler, |
| 14 |
the mediator and the task pool. All these components runs in parallel working |
| 15 |
with two data structures: the ready queue and the ETA schedule. |
| 16 |
|
| 17 |
Data structures |
| 18 |
=============== |
| 19 |
|
| 20 |
timer |
| 21 |
----- |
| 22 |
|
| 23 |
The timer uses :mod:`heapq` to schedule internal functions. |
| 24 |
It's very efficient and can handle hundred of thousands of entries. |
| 25 |
|
| 26 |
|
| 27 |
Components |
| 28 |
========== |
| 29 |
|
| 30 |
Consumer |
| 31 |
-------- |
| 32 |
|
| 33 |
Receives messages from the broker using :pypi:`Kombu`. |
| 34 |
|
| 35 |
When a message is received it's converted into a |
| 36 |
:class:`celery.worker.request.Request` object. |
| 37 |
|
| 38 |
Tasks with an ETA, or rate-limit are entered into the `timer`, |
| 39 |
messages that can be immediately processed are sent to the execution pool. |
| 40 |
|
| 41 |
ETA and rate-limit when used together will result in the rate limit being |
| 42 |
observed with the task being scheduled after the ETA. |
| 43 |
|
| 44 |
Timer |
| 45 |
----- |
| 46 |
|
| 47 |
The timer schedules internal functions, like cleanup and internal monitoring, |
| 48 |
but also it schedules ETA tasks and rate limited tasks. |
| 49 |
If the scheduled tasks ETA has passed it is moved to the execution pool. |
| 50 |
|
| 51 |
TaskPool |
| 52 |
-------- |
| 53 |
|
| 54 |
This is a slightly modified :class:`multiprocessing.Pool`. |
| 55 |
It mostly works the same way, except it makes sure all of the workers |
| 56 |
are running at all times. If a worker is missing, it replaces |
| 57 |
it with a new one. |