python 2线程锁、rlock和条件的c实现
cthreading的Python项目详细描述
cthreading在c中实现python 2锁、rlock和条件,加速 向上线程同步并降低CPU使用率。
性能
cthreading消除了线程的开销。rlock和 threading.condition在python 2中用纯python实现。 特别是,threading.condition是使用轮询实现的。在 python 3threading.condition是在没有轮询的情况下实现的; cthreading在c中实现了类似的设计。
$ time python whispers.py -m cthreading real 0m2.664s user 0m2.965s sys 0m0.808s $ time python3 whispers.py real 0m9.664s user 0m8.949s sys 0m1.812s $ time python whispers.py real 0m14.914s user 0m16.986s sys 0m12.690s $ time python whispers.py -m pthreading real 0m20.169s user 0m23.062s sys 0m17.022s
您的应用程序不太可能有类似的工作负载;不要期望 这一改进。
有关详细信息,请参见https://github.com/nirs/cthreading/wiki/performance。
用法
在任何其他模块和monkeypatch之前导入cthreading 线程模块。从这里开始,threading.lock、threading.rlock和 线程。条件正在使用cthreading。
importcthreadingcthreading.monkeypatch()
注意:如果线程模块 在调用cthreading.monkeypatch()之前导入。
测试平台
x86_64
- Fedora 22/Python2.7.10
- RHEL 7.2/Python 2.7.5
- RHEL 7.2/Python 2.7.10+(上游)
- RHEL 7.1/Python 2.7.5
- RHEL 6.7/python-2.6.6
- ubuntu 14.04服务器/python 2.7.6
- ubuntu 12.04服务器/python 2.7.3(python回归测试不可用)
- ubuntu 12.04/python 2.6.9(travis容器)
- ubuntu 12.04/python 2.7.9(travis容器)
功率8e
- RHEL 7.2/Python 2.7.5
- RHEL 7.2/Python 2.7.10+(上游)
黑客攻击
对于基于RPM的发行版:
yum install python-devel python-test
对于基于deb的分布:
apt-get install python-dev libpython2.7-testsuite
安装python包:
pip install pytest pytest-timeout yappi==0.93
建立并运行快速测试:
make
在提交修补程序之前,请运行python回归测试套件:
make regrtest
有关详细信息,请查看生成文件。
类似项目
- pthreading-使用 pthread_mutex和pthread_cond api直接通过cTypes。这个 引入未定义的行为,实际上更慢并增加CPU 与原始python实现相比,在大多数情况下使用。