Greenlet API的纯python实现 [英] Pure python implementation of greenlet API

查看:86
本文介绍了Greenlet API的纯python实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

gevent和eventlet将 greenlet 包用于异步IO.它被编写为C扩展,因此不适用于Jython或IronPython.如果性能无关紧要,那么在纯Python中实现greenlet API的最简单方法是什么.

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.

一个简单的例子:

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).

Should print 12, 56, 34 (and not 78).

推荐答案

这种事情可以通过从2.5版开始内置在标准Python发行版中的协同例程来实现.如果IronPython和co完全兼容所有Python 2.5功能(我相信它们),那么您应该可以使用此惯用法.

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.

有关如何使用它们的更多信息,请参见这篇文章 :)具体来说,您将对 PDF 感兴趣,作者仅使用提供类似功能的纯Python构建系统无堆栈Python或Greenlet模块的功能.

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.

您可能还想查看 Gogen 此页面,以对cogen的处理方式.

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.

请注意,此处的协同例程实现与greenlet实现之间存在一些差异.纯粹的python实现都使用某种外部调度程序,但思想本质上是相同的:它们为您提供了一种运行轻量级合作任务的方式,而无需求助于线程.另外,链接到上面的两个框架都非常适合异步IO,就像greenlet本身一样.

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.

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

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

它比greenlet版本更为明确(例如,使用WaitForSignal显式创建恢复点),但是您应该了解一般想法.

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.

我刚刚确认可以使用jython

edit: I just confirmed that this works using jython

KidA% jython test.py 
12
56
34

这篇关于Greenlet API的纯python实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆