跳转到主要内容

Python的轻量级协作微线程

项目描述

https://badge.fury.io/py/fibers.png

概述

Fibers是Python中进行协作多任务的轻量级原语。它们提供了运行可以暂停和恢复的代码片段的手段。与抢占式调度的线程不同,fibers是协作调度的,也就是说,在任何给定时间点,只有一个fibers会运行,并且没有其他fibers会运行,直到用户明确决定。

创建fibers时,它们不会自动运行。必须通过将fibers“切换”到它才能运行。fibers可以通过使用switchthrow函数将控制权切换到其他fibers,这两个函数分别切换控制权或在目标fibers中引发异常。

示例

import fibers

def func1():
    print "1"
    f2.switch()
    print "3"
    f2.switch()

def func2():
    print "2"
    f1.switch()
    print "4"

f1 = fibers.Fiber(target=func1)
f2 = fibers.Fiber(target=func2)
f1.switch()

上面的示例将打印“1 2 3 4”,但是结果是通过对2个fibers的协作工作,它们互相切换控制权而得到的。

CI状态

https://secure.travis-ci.org/saghul/python-fibers.png?branch=master https://ci.appveyor.com/api/projects/status/9f4h0wk797i4vc0k?svg=true

文档

http://readthedocs.org/docs/python-fibers/

安装

fibers可以通过以下方式通过pip安装

pip install fibers

构建

获取源代码

git clone https://github.com/saghul/python-fibers

Linux

./build_inplace

Mac OSX

(XCode needs to be installed)
export ARCHFLAGS="-arch x86_64"
./build_inplace

Microsoft Windows

python setup.py build_ext --inplace

运行测试套件

可以使用nose运行测试套件

nosetests -v

作者

Saúl Ibarra Corretgé <saghul@gmail.com>

本项目的实现离不开在greenlet和stacklet(PyPy项目的一部分PyPy)项目中所做的前期工作。

许可证

除非文件中另有说明,否则fibers使用MIT许可证,请检查LICENSE文件。

支持的Python版本

支持Python >= 2.7和>= 3.3。其他较老的Python版本可能也可以工作,但它们未经过积极测试。支持CPython和PyPy。

支持的架构

支持x86、x86-64、ARM、MIPS64、PPC64和s390x架构。

贡献

如果您想做出贡献,请fork项目,制作补丁并提交pull request。请查看周围的代码,并确保您的代码风格与之相似。

项目详情


下载文件

下载适用于您平台文件的文件。如果您不确定选择哪一个,请了解更多关于安装包的信息。

源码分发

fibers-1.1.0.tar.gz (36.1 kB 查看哈希值)

上传时间 源码

构建版本

fibers-1.1.0-cp36-cp36m-win_amd64.whl (16.3 kB 查看哈希值)

上传时间 CPython 3.6m Windows x86-64

fibers-1.1.0-cp36-cp36m-win32.whl (15.0 kB 查看哈希值)

上传时间 CPython 3.6m Windows x86

fibers-1.1.0-cp35-cp35m-win_amd64.whl (16.3 kB 查看哈希值)

上传时间 CPython 3.5m Windows x86-64

fibers-1.1.0-cp35-cp35m-win32.whl (15.0 kB 查看哈希值)

上传时间 CPython 3.5m Windows x86

fibers-1.1.0-cp34-cp34m-win_amd64.whl (14.2 kB 查看哈希值)

上传时间 CPython 3.4m Windows x86-64

fibers-1.1.0-cp34-cp34m-win32.whl (13.9 kB 查看哈希值)

上传时间 CPython 3.4m Windows x86

fibers-1.1.0-cp33-cp33m-win_amd64.whl (14.2 kB 查看哈希值)

上传于 CPython 3.3m Windows x86-64

fibers-1.1.0-cp33-cp33m-win32.whl (13.9 kB 查看哈希值)

上传于 CPython 3.3m Windows x86

fibers-1.1.0-cp27-cp27m-win_amd64.whl (14.3 kB 查看哈希值)

上传于 CPython 2.7m Windows x86-64

fibers-1.1.0-cp27-cp27m-win32.whl (14.0 kB 查看哈希值)

上传于 CPython 2.7m Windows x86

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面