-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
almamun-turing
wants to merge
2
commits into
celery:main
Choose a base branch
from
almamun-turing:add_doc
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
optimizing | ||
debugging | ||
concurrency/index | ||
workerpool | ||
signals | ||
testing | ||
extending | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.