在Twisted上使用WSGI

2024-04-26 10:09:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我可以一起使用Twisted和mod wsgi来尝试性能上的提升吗?在

既然我不打算reactor.listenTCP(…),如何使用twisted的异步方法?公司名称:

我所做的:

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

Tags: modwsgioutputapplicationresponsestatusenvirontwisted
1条回答
网友
1楼 · 发布于 2024-04-26 10:09:44

你不能

如果您想使用Twisted作为WSGI容器,那么就使用Twisted。如果您想使用Apache,那么就使用Apache。但是,如果您使用Apache作为WSGI容器,那么您将无法使用Twisted的特性,因为Twisted的事件循环与Apache进行网络I/O的方式不兼容

您在代码示例中所做的是双重无意义的,因为WSGIResource是Twisted的HTTP服务器和WSGI之间的粘合剂;即使您可以通过mod_wsgi将Twisted塞进正在运行的apachehttpd进程中,您也不需要WSGIResource,因为Apache将填补这个角色。在

相关问题 更多 >