[gevent源码分析] libev cython绑定core.pyx

本文深入探讨gevent的核心部分,它基于libev并利用Cython进行优化。文章首先介绍了Cython的基础知识,然后详细分析了core.pyx源码,对比了libev的Python封装库pyev,指出pyev使用C编写,代码复杂度较高。通过学习,读者将能更好地理解gevent如何结合libev和Cython提升性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

gevent core就是封装了libev,使用了cython的语法,感兴趣童鞋可以好好研究研究。其实libev是有python的封装

pyev(https://pythonhosted.org/pyev/),不过pyev是使用C来写扩展的,代码巨复杂。在看core.pyx代码之前先学习一下

core.pyx用到的cython知识。


一: cython基础知识

1.cdef, def, cpdef的区别
cdef用于定义C中的函数,变量,如cdef int i;而def知识python中的函数定义方法,一般只是为了提供python的访问。
cdef定义的函数,变量在python环境中是访问不了的,要么提供一个def的包装方法,要么用cpdef。cpdef只用于定义函数,
速度比cdef稍慢,主要因为cpdef定义的类函数支持重载,调用的时候需要查找虚函数表,cpdef同时生成供cython和python
调用的函数

2.明确的类型声明
为了提高速度和可读性,cython建议所有的变量加上类型,包括python中的类型。如定义列表,cdef list result;
如果是类可用cdef object p;

3.class如何定义,public的用法
我们看下core.pyx提供的class loop的定义
cdef public class loop [object PyGeventLoopObject, type PyGeventLoop_Type]:
    cdef libev.ev_loop* _ptr
    cdef public object error_handler
    cdef libev.ev_prepare _prepare
    cdef public list _callbacks
    cdef libev.ev_timer _timer0
你可能好奇,loop后面的中括号是干嘛用的?我们想想python中int类型的PyIntObject,
typedef struct {
    PyObject_HEAD
    long ob_ival;
} PyIntObject;
同样我们会生PyGeventLoopObject对象
Requirement already satisfied: setuptools>=0.7 in ./.venv/lib/python3.9/site-packages (from APScheduler==3.7.0->-r requirements.txt (line 4)) (80.9.0) Requirement already satisfied: gevent in ./.venv/lib/python3.9/site-packages (from grequests==0.6.0->-r requirements.txt (line 40)) (25.5.1) Requirement already satisfied: numpy>=1.15.4 in ./.venv/lib/python3.9/site-packages (from pandas==1.1.5->-r requirements.txt (line 58)) (2.0.2) Requirement already satisfied: scikit-learn in ./.venv/lib/python3.9/site-packages (from sklearn==0.0->-r requirements.txt (line 79)) (1.6.1) INFO: pip is looking at multiple versions of gevent to determine which version is compatible with other requirements. This could take a while. Collecting gevent (from grequests==0.6.0->-r requirements.txt (line 40)) Downloading gevent-25.4.2.tar.gz (6.3 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 6.3/6.3 MB 10.8 MB/s eta 0:00:00 Installing build dependencies ... done Getting requirements to build wheel ... done Preparing metadata (pyproject.toml) ... done Downloading gevent-25.4.1.tar.gz (6.3 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 6.3/6.3 MB 7.4 MB/s eta 0:00:00 Installing build dependencies ... done Getting requirements to build wheel ... done Preparing metadata (pyproject.toml) ... done Downloading gevent-24.11.1.tar.gz (6.0 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 6.0/6.0 MB 11.8 MB/s eta 0:00:00 Installing build dependencies ... done Getting requirements to build wheel ... done Preparing metadata (pyproject.toml) ... done Downloading gevent-24.10.3.tar.gz (6.1 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 6.1/6.1 MB 7.4 MB/s eta 0:00:00 Installing build dependencies ... done Getting requirements to build wheel ... error error: subprocess-exited-with-error × Getting requirements to build wheel did not run successfully. │ exit code: 1 ╰─> [39 lines of output] Compiling src/gevent/resolver/cares.pyx because it changed. [1/1] Cythonizing src/gevent/resolver/cares.pyx Error compiling Cython file: ------------------------------------------------------------ ... cdef tuple integer_types if sys.version_info[0] >= 3: integer_types = int, else: integer_types = (int, long) ^ ------------------------------------------------------------ src/gevent/libev/corecext.pyx:69:26: undeclared name not builtin: long Compiling src/gevent/libev/corecext.pyx because it changed. [1/1] Cythonizing src/gevent/libev/corecext.pyx Traceback (most recent call last): File "/Users/didi/code/doraemon-api/.venv/lib/python3.9/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 389, in <module> main() File "/Users/didi/code/doraemon-api/.venv/lib/python3.9/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 373, in main json_out["return_val"] = hook(**hook_input["kwargs"]) File "/Users/didi/code/doraemon-api/.venv/lib/python3.9/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 143, in get_requires_for_build_wheel return hook(config_settings) File "/private/var/folders/cf/1231crpx4hsf3vf_6wtb6ybw0000ks/T/pip-build-env-i9_0xwq3/overlay/lib/python3.9/site-packages/setuptools/build_meta.py", line 331, in get_requires_for_build_wheel return self._get_build_requires(config_settings, requirements=[]) File "/private/var/folders/cf/1231crpx4hsf3vf_6wtb6ybw0000ks/T/pip-build-env-i9_0xwq3/overlay/lib/python3.9/site-packages/setuptools/build_meta.py", line 301, in _get_build_requires self.run_setup() File "/private/var/folders/cf/1231crpx4hsf3vf_6wtb6ybw0000ks/T/pip-build-env-i9_0xwq3/overlay/lib/python3.9/site-packages/setuptools/build_meta.py", line 317, in run_setup exec(code, locals()) File "<string>", line 54, in <module> File "/private/var/folders/cf/1231crpx4hsf3vf_6wtb6ybw0000ks/T/pip-install-p6p1o97m/gevent_804a446da2d9472bb3cfb9a391265401/_setuputils.py", line 249, in cythonize1 new_ext = cythonize( File "/private/var/folders/cf/1231crpx4hsf3vf_6wtb6ybw0000ks/T/pip-build-env-i9_0xwq3/overlay/lib/python3.9/site-packages/Cython/Build/Dependencies.py", line 1154, in cythonize cythonize_one(*args) File "/private/var/folders/cf/1231crpx4hsf3vf_6wtb6ybw0000ks/T/pip-build-env-i9_0xwq3/overlay/lib/python3.9/site-packages/Cython/Build/Dependencies.py", line 1298, in cythonize_one raise CompileError(None, pyx_file) Cython.Compiler.Errors.CompileError: src/gevent/libev/corecext.pyx [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: subprocess-exited-with-error × Getting requirements to build wheel did not run successfully. │ exit code: 1 ╰─> See above for output. note: This error originates from a subprocess, and is likely not a problem with pip.
最新发布
06-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值