greenlet API的纯python实现

发布于 2024-09-03 16:31:21 字数 456 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

澜川若宁 2024-09-10 16:31:21

这种事情可以通过自 2.5 版本以来内置于标准 Python 发行版中的协同例程来实现。如果 IronPython 和 co 完全兼容所有 Python 2.5 功能(我相信它们是),您应该能够使用这个习惯用法。

有关如何使用它们的更多信息,请参阅这篇文章 :) 具体来说,您会感兴趣PDF,其中作者仅使用纯 Python 构建了一个系统,该系统提供了与以下任一系统类似的功能stackless Python 或 Greenlet 模块。

您可能还想查看 GogenKamelia 获取想法:这些项目都有纯 python 协程实现,您可以采用或用作您自己的实现的参考。请查看此页面,了解有关cogen 的做事方式。

请注意,此处的协同例程实现与 Greenlet 实现之间存在一些差异。纯 python 实现都使用某种外部调度程序,但其思想本质上是相同的:它们为您提供了一种运行轻量级协作任务的方法,而无需诉诸线程。此外,上面链接的两个框架都面向异步 IO,非常类似于 greenlet 本身。

这是您发布但使用 cogen 重写的示例:

from cogen.core.coroutines import coroutine
from cogen.core.schedulers import Scheduler
from cogen.core import events

@coroutine
def test1():
    print 12
    yield events.AddCoro(test2)
    yield events.WaitForSignal(test1)
    print 34

@coroutine
def test2():
    print 56
    yield events.Signal(test1)
    yield events.WaitForSignal(test2)
    print 78

sched = Scheduler()
sched.add(test1)
sched.run()

>>> 12
>>> 56
>>> 34

它比 greenlet 版本更明确一些(例如使用 WaitForSignal 显式创建简历点)但你应该了解总体思路。

编辑:我刚刚确认这可以使用 jython

KidA% jython test.py 
12
56
34

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 greenletimplementation. 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 like greenlet itself.

Here's the example you posted but rewritten using cogen:

from cogen.core.coroutines import coroutine
from cogen.core.schedulers import Scheduler
from cogen.core import events

@coroutine
def test1():
    print 12
    yield events.AddCoro(test2)
    yield events.WaitForSignal(test1)
    print 34

@coroutine
def test2():
    print 56
    yield events.Signal(test1)
    yield events.WaitForSignal(test2)
    print 78

sched = Scheduler()
sched.add(test1)
sched.run()

>>> 12
>>> 56
>>> 34

It's a little more explicit than the greenlet version (for example using WaitForSignal to explicitly create a resume point) but you should get the general idea.

edit: I just confirmed that this works using jython

KidA% jython test.py 
12
56
34
旧夏天 2024-09-10 16:31:21

不可能用纯 Python 实现 greenlet。

更新:

  • 使用线程伪造 greenlet API 确实是可行的,即使对于所有实际目的来说完全无用,
  • 生成器也不能用于此目的,因为它们只保存单个帧的状态。 Greenlets 保存整个堆栈。这意味着 gevent 可以使用在标准套接字之上实现的任何协议(例如 httplib 和 urllib2 模块)。基于生成器的框架需要在软件的所有层中使用生成器,因此 httplib 和大量其他包被丢弃。

It's not possible to implement greenlet in pure Python.

UPDATE:

  • faking greenlet API with threads could be indeed doable, even if completely useless for all practical purposes
  • generators cannot be used for this as they only save the state of a single frame. Greenlets save the whole stack. This means gevent can use any protocol implemented on top of the standard socket (e.g. httplib and urllib2 modules). Generator-based frameworks require generators in all layers of your software, so httplib and tons of other packages are thrown away.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文