| 1 |
.. _concurrency-eventlet: |
| 2 |
|
| 3 |
=========================== |
| 4 |
Concurrency with Eventlet |
| 5 |
=========================== |
| 6 |
|
| 7 |
.. _eventlet-introduction: |
| 8 |
|
| 9 |
Introduction |
| 10 |
============ |
| 11 |
|
| 12 |
The `Eventlet`_ homepage describes it as |
| 13 |
a concurrent networking library for Python that allows you to |
| 14 |
change how you run your code, not how you write it. |
| 15 |
|
| 16 |
* It uses `epoll(4)`_ or `libevent`_ for |
| 17 |
`highly scalable non-blocking I/O`_. |
| 18 |
* `Coroutines`_ ensure that the developer uses a blocking style of |
| 19 |
programming that's similar to threading, but provide the benefits of |
| 20 |
non-blocking I/O. |
| 21 |
* The event dispatch is implicit: meaning you can easily use Eventlet |
| 22 |
from the Python interpreter, or as a small part of a larger application. |
| 23 |
|
| 24 |
|
| 25 |
Celery supports Eventlet as an alternative execution pool implementation and |
| 26 |
in some cases superior to prefork. However, you need to ensure one task doesn't |
| 27 |
block the event loop too long. Generally, CPU-bound operations don't go well |
| 28 |
with Eventlet. Also note that some libraries, usually with C extensions, |
| 29 |
cannot be monkeypatched and therefore cannot benefit from using Eventlet. |
| 30 |
Please refer to their documentation if you are not sure. For example, pylibmc |
| 31 |
does not allow cooperation with Eventlet but psycopg2 does when both of them |
| 32 |
are libraries with C extensions. |
| 33 |
|
| 34 |
|
| 35 |
The prefork pool can take use of multiple processes, but how many is |
| 36 |
often limited to a few processes per CPU. With Eventlet you can efficiently |
| 37 |
spawn hundreds, or thousands of green threads. In an informal test with a |
| 38 |
feed hub system the Eventlet pool could fetch and process hundreds of feeds |
| 39 |
every second, while the prefork pool spent 14 seconds processing 100 |
| 40 |
feeds. Note that this is one of the applications async I/O is especially good |
| 41 |
at (asynchronous HTTP requests). You may want a mix of both Eventlet and |
| 42 |
prefork workers, and route tasks according to compatibility or |
| 43 |
what works best. |
| 44 |
|
| 45 |
Enabling Eventlet |
| 46 |
================= |
| 47 |
|
| 48 |
You can enable the Eventlet pool by using the :option:`celery worker -P` |
| 49 |
worker option. |
| 50 |
|
| 51 |
.. code-block:: console |
| 52 |
|
| 53 |
$ celery -A proj worker -P eventlet -c 1000 |
| 54 |
|
| 55 |
.. _eventlet-examples: |
| 56 |
|
| 57 |
Examples |
| 58 |
======== |
| 59 |
|
| 60 |
See the `Eventlet examples`_ directory in the Celery distribution for |
| 61 |
some examples taking use of Eventlet support. |
| 62 |
|
| 63 |
.. _`Eventlet`: http://eventlet.net |
| 64 |
.. _`epoll(4)`: http://linux.die.net/man/4/epoll |
| 65 |
.. _`libevent`: http://monkey.org/~provos/libevent/ |
| 66 |
.. _`highly scalable non-blocking I/O`: |
| 67 |
https://en.wikipedia.org/wiki/Asynchronous_I/O#Select.28.2Fpoll.29_loops |
| 68 |
.. _`Coroutines`: https://en.wikipedia.org/wiki/Coroutine |
| 69 |
.. _`Eventlet examples`: |
| 70 |
https://github.com/celery/celery/tree/main/examples/eventlet |
| 71 |
|