Tornado协同程序在Cython中不起作用

时间:2015-05-25 09:13:21

标签: python generator tornado cython coroutine

此代码在Python 3.4.3中使用Tornado 4.1工作 - 它休眠1秒钟然后打印“Hello World!123”。但是当使用Cython编译时(我尝试了版本0.20.1post0和0.23dev),它什么也没做。

import tornado.ioloop
import datetime
from tornado import gen

@gen.coroutine
def test():
    yield gen.Task(ioloop.add_timeout, datetime.timedelta(seconds=1))
    return 123

@gen.coroutine
def hello_world():
    print('Hello World! {}'.format((yield test())))

ioloop = tornado.ioloop.IOLoop().instance()
ioloop.run_sync(hello_world)

我用来构建和运行Cython版本的命令:

cython --embed -o hello.c hello.py
gcc -shared -fPIC -O0 -Wall -I/usr/include/python3.4 -o hello.so hello.c
python -c 'import hello'

1 个答案:

答案 0 :(得分:3)

更新:从Tornado 4.3开始,本机支持Cython协同程序。以下解决方法仅适用于旧版本的Tornado。

Cython目前不支持龙卷风协同程序。主要问题是Cython编译的生成器没有通过preg_replace('/(https?:\/\/[^ ]+?(?:\.jpg|\.png|\.gif))/', '<img src="$1" alt="$1" />', $string); (上次我看到没有其他类可以代替使用)。

对此最好的解决方法是更改​​Cython以为生成器添加公共基类,但作为快速黑客,我已经在isinstance(types.GeneratorType)的此修补程序中取得了一些成功:

tornado/gen.py