gevent 并发¶
Concurrency with gevent
简介¶
Introduction
gevent 的主页将其描述为一个基于协程(coroutine)_ 的 Python 网络库,使用 greenlet 提供基于 libev 或 libuv 事件循环的高层同步 API。
其特性包括:
基于 greenlet 的轻量级执行单元;
支持 SSL 的协作式 socket: 协作式 socket,含 SSL 支持
通过线程池、dnspython 或 c-ares 实现的 协作式 DNS 查询;
通过 monkey patching 让第三方模块支持协作式 I/O 的工具: monkey patch 工具;
支持 TCP/UDP/HTTP 服务;
子进程支持(通过 gevent.subprocess);
线程池支持。
gevent 受 eventlet 启发而来,但提供更一致的 API、更简洁的实现以及更好的性能。 你可以了解其他人 为何 使用 gevent,并查看 基于 gevent 的开源项目列表。
The gevent homepage describes it a coroutine -based Python networking library that uses greenlet to provide a high-level synchronous API on top of the libev or libuv event loop.
Features include:
Lightweight execution units based on greenlets.
API that reuses concepts from the Python standard library (for examples there are events and queues).
Cooperative DNS queries performed through a threadpool, dnspython, or c-ares.
Monkey patching utility to get 3rd party modules to become cooperative
TCP/UDP/HTTP servers
Subprocess support (through gevent.subprocess)
Thread pools
gevent is inspired by eventlet but features a more consistent API, simpler implementation and better performance. Read why others use gevent and check out the list of the open source projects based on gevent.
启用 gevent¶
Enabling gevent
你可以使用 celery worker -P gevent
或
celery worker --pool=gevent
选项启用 gevent worker 池:
$ celery -A proj worker -P gevent -c 1000
You can enable the gevent pool by using the
celery worker -P gevent
or celery worker --pool=gevent
worker option.
$ celery -A proj worker -P gevent -c 1000
示例¶
Examples
Celery 发布包中提供了使用 gevent 的示例,请参阅 gevent examples 目录。
See the gevent examples directory in the Celery distribution for some examples taking use of Eventlet support.
已知问题¶
Known issues
在 Python 3.11 与 gevent 一起使用时存在已知问题。 该问题已被记录在 此处 并在 gevent issue 中处理。升级到 greenlet 3.0 即可解决该问题。
There is a known issue using python 3.11 and gevent. The issue is documented here and addressed in a gevent issue. Upgrading to greenlet 3.0 solves it.