Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation for worker pools #8805

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/userguide/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
optimizing
debugging
concurrency/index
workerpool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought, this isn’t the best place.
I think inside the https://docs.celeryq.dev/en/latest/userguide/workers.html would make more sense.

signals
testing
extending
Expand Down
105 changes: 105 additions & 0 deletions docs/userguide/wokerpool.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
.. _worker-pool:

==============================
Nusnus marked this conversation as resolved.
Show resolved Hide resolved
Different type of Worker Pool
==============================
Nusnus marked this conversation as resolved.
Show resolved Hide resolved

.. contents::
:local:

.. _worker-pool-introduction:

Introduction
============
The concept of a worker pool involves the utilization of a group of worker processes (or threads)
to execute tasks in parallel. A worker pool constitutes of several worker instances, each capable
of picking up and executing tasks independently. This model provides several benefits which enhance
the performance and flexibility of a Celery-based application.

Different type of worker pools
==============================
There are four different types of worker pools available in Celery.

.. _worker-pool-prefork:

Prefork Pool
------------
The default execution pool in Celery is the prefork pool, which leverages the multiprocessing module of
Python to efficiently manage worker processes. This prefork pool model is particularly suited for CPU-bound tasks,
as it spawns multiple worker processes that can run in parallel, each taking advantage of individual CPU cores.
This multi-process approach is one of the reasons why the prefork pool delivers high performance and stability,
making it an optimal choice for many use cases.

The concurrency level for CPU-bound tasks is optimally determined by the number of CPU cores present on the system,
as this sets the limit for how many processes can effectively run in parallel. In recognition of this constraint,
Celery automatically defaults to the number of available CPU cores to set its concurrency level when the `--concurrency`
option is not explicitly specified by the user.


.. code-block:: console

$ celery worker -A proj
# Or
$ celery worker -A proj --pool=prefork


.. _worker-pool-thread:

Thread Pool
-----------
Thread pool is a special type of worker pool that utilizes Python's ThreadPoolExecutor.
This type of pool creates genuine OS-level threads, which are managed by the operating system's kernel.
This thread pool model is particularly suited for I/O-bound tasks, as it spawns multiple worker threads that can run
in parallel. A task is considered I/O-bound when it primarily awaits the completion of Input/Output operations.

Celery automatically set the number of available CPU cores as default concurrency when the `--concurrency` option is not explicitly specified by the user.

.. code-block:: console

$ celery worker -A proj --pool=threads


.. _worker-pool-eventlet:

Eventlet and Gevent Pool
------------------------
Eventlet and gevent are both lightweight concurrency libraries for Python that provide a framework for asynchronous networking. In the context of Celery, they offer alternative worker pool implementations that are well-suited for handling large numbers of I/O-bound tasks.

The Eventlet worker pool uses the Eventlet library to spawn a pool of green threads (also known as "greenlets").
These greenlets are cooperatively scheduled lightweight threads that run on a single OS thread, allowing for high
concurrency by switching contexts whenever I/O operations are pending, thus making efficient use of downtime where
a traditional thread might simply block.

Similarly, the gevent worker pool leverages the gevent library, which also uses greenlets and a cooperative multitasking model.
Gevent includes a fast event loop backed by libev or libuv that can handle many simultaneous connections, providing an effective
way to run network applications and other I/O-bound tasks with a high degree of concurrency.

Both Eventlet and gevent worker pools offer a scalable solution for network applications or other I/O-bound workloads
that involve waiting for external operations, such as HTTP requests or database queries. By enabling non-blocking I/O
operations through asynchronous execution, these pools can handle many tasks concurrently within a single process,
thus reducing memory overhead and taking full advantage of the event-driven programming model.

.. code-block:: console

$ celery -A proj worker --pool=eventlet -c 1000
$ celery -A proj worker --pool=gevent -c 1000


.. _worker-pool-solo:

Solo Pool
---------
Solo pool is a type of worker pool in Celery, which means it uses only one worker process.
This can be useful for debugging and development purposes as it allows you to see
the full traceback of any errors that occur during task execution.
It's particularly useful when you want to isolate issues related to specific tasks or
when you need to ensure that a certain task is executed in isolation. The solo pool is
also beneficial for testing, as it provides a controlled environment where you can test
your code without the overhead of multiple workers. However, keep in mind that using the
solo pool might limit the performance and scalability of your application since only one
worker process will be available to handle tasks.

.. code-block:: console

$ celery worker -A proj --pool=solo