greenlet API的纯python实现
gevent 和 eventlet 使用 greenlet 包进行异步 IO。它是作为 C 扩展编写的,因此不能与 Jython 或 IronPython 一起使用。如果不关心性能,那么用纯 Python 实现 greenlet API 最简单的方法是什么。
一个简单的例子:
def test1():
print 12
gr2.switch()
print 34
def test2():
print 56
gr1.switch()
print 78
gr1 = greenlet(test1)
gr2 = greenlet(test2)
gr1.switch()
应该打印 12、56、34(而不是 78)。
The greenlet package is used by gevent and eventlet for asynchronous IO. It is written as a C-extension and therefore doesn't work with Jython or IronPython. If performance is of no concern, what is the easiest approach to implementing the greenlet API in pure Python.
A simple example:
def test1():
print 12
gr2.switch()
print 34
def test2():
print 56
gr1.switch()
print 78
gr1 = greenlet(test1)
gr2 = greenlet(test2)
gr1.switch()
Should print 12, 56, 34 (and not 78).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这种事情可以通过自 2.5 版本以来内置于标准 Python 发行版中的协同例程来实现。如果 IronPython 和 co 完全兼容所有 Python 2.5 功能(我相信它们是),您应该能够使用这个习惯用法。
有关如何使用它们的更多信息,请参阅这篇文章 :) 具体来说,您会感兴趣PDF,其中作者仅使用纯 Python 构建了一个系统,该系统提供了与以下任一系统类似的功能stackless Python 或 Greenlet 模块。
您可能还想查看 Gogen 或 Kamelia 获取想法:这些项目都有纯 python 协程实现,您可以采用或用作您自己的实现的参考。请查看此页面,了解有关
cogen
的做事方式。请注意,此处的协同例程实现与 Greenlet 实现之间存在一些差异。纯 python 实现都使用某种外部调度程序,但其思想本质上是相同的:它们为您提供了一种运行轻量级协作任务的方法,而无需诉诸线程。此外,上面链接的两个框架都面向异步 IO,非常类似于 greenlet 本身。
这是您发布但使用
cogen
重写的示例:它比
greenlet
版本更明确一些(例如使用WaitForSignal
显式创建简历点)但你应该了解总体思路。编辑:我刚刚确认这可以使用 jython
This kind of thing can be achieved with co-routines which have been built-in to the standard Python distribution since version 2.5. If IronPython and co are fully compliant with all Python 2.5 features (I believe they are) you should be able to use this idiom.
See this post for more information on how they can be used :) Specifically, you'll be interested in the PDF where the author builds a system using nothing but pure Python that provides similar capabilities to either stackless Python or the Greenlet module.
You may also want to look either Gogen or Kamelia for ideas: these projects both have pure python coroutine implementations which you could either adopt or use as a reference for your own implementation. Take a look at this page for a gentle introduction to the
cogen
way of doing things.Note there are some differences between the co-routine implementations here and the
greenlet
implementation. The pure python implementations all use some kind of external scheduler but the idea is essentially the same: they provide you with a way to run lightweight, co-operative tasks without the need to resort to threads. Additionally both the frameworks linked to above are geared towards asynchronous IO very much likegreenlet
itself.Here's the example you posted but rewritten using
cogen
:It's a little more explicit than the
greenlet
version (for example usingWaitForSignal
to explicitly create a resume point) but you should get the general idea.edit: I just confirmed that this works using jython
不可能用纯 Python 实现 greenlet。
更新:
It's not possible to implement greenlet in pure Python.
UPDATE: