2012-02-17 84 views
1

我可以一起使用Twisted和mod_wsgi来尝试性能上的增益吗?Twisted上使用WSGI

由于我不是开始reactor.listenTCP(...),我该如何使用的扭曲的异步方法?:

我曾尝试:

> server.wsgi

def application(environ, start_response): 
    status = '200 OK' 
    output = 'Pong!' 
    response_headers = [('Content-type', 'text/plain'), 
         ('Content-Length', str(len(output)))] 
    start_response(status, response_headers) 
    # How do I call a twisted async method from here?! 
    # like deferToThread(object.send, environ). 
    return [output] 

resource = WSGIResource(reactor, reactor.getThreadPool(), application) 

回答

8

你不行。

如果要将Twisted用作WSGI容器,请使用Twisted。如果你想使用Apache,那么使用Apache。但是,如果将Apache用作WSGI容器,那么您将无法使用Twisted的功能,因为Twisted的事件循环与Apache执行网络I/O的方式不兼容。

你在代码示例中做什么是双重无意义的,因为WSGIResource是Twisted的HTTP服务器和WSGI之间的粘合剂;即使你能以某种方式阻塞通过mod_wsgi Twisted到一个正在运行的Apache HTTPD进程中,你也不需要WSGIResource,因为apache会填补这个角色。