Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using WSGI on Twisted

Can I use Twisted and mod_wsgi together, to try some gain in performance?

Since I am not starting a reactor.listenTCP(...), how do I use the async methods of twisted?:

What I have tried:

> 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)
like image 573
joaoricardo000 Avatar asked Mar 30 '26 22:03

joaoricardo000


1 Answers

You can't.

If you want to use Twisted as your WSGI container, then use Twisted. If you want to use Apache, then use Apache. However, if you use Apache as your WSGI container, then you will not be able to use features from Twisted, because Twisted's event loop is not compatible with the way Apache does network I/O.

What you're doing in the code example is doubly meaningless, as WSGIResource is the glue between Twisted's HTTP server and WSGI; even if you could somehow jam Twisted into a running Apache HTTPD process via mod_wsgi, you would not need a WSGIResource, since apache would be filling that role.

like image 151
Glyph Avatar answered Apr 01 '26 13:04

Glyph



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!