2012-02-17 77 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會填補這個角色。