eventlet.rst 2.6 KB

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