[Twisted-web] Single-threaded WSGIResource

Cristiano Paris cristiano.paris at gmail.com
Tue Feb 2 16:58:45 EST 2010


Hi,

I'm playing with Django (1.1.1) under Twisted Web (9.0.0) through the
WSGI interface. When using my web app under the test server with
sqlite3 as the DBMS, I'm having troubles since in-memory sqlite3 DBs
can't be shared among threads.

The solution I came up with was to run the WSGIResource in the main
thread. In order to do so, I subclassed the WSGIResource as follows:

-----
from twisted.web.wsgi import WSGIResource,_WSGIResponse
from twisted.web.server import NOT_DONE_YET

class MainThreadWSGIResource(WSGIResource):
  def __init__(self,reactor,app):
    WSGIResource.__init__(self,reactor,None,app)

  def render(self,request):
    response = _WSGIResponse(
        self._reactor, self._threadpool, self._application, request)
    response.run()
    return NOT_DONE_YET
-----

Basically, I replaced "response.start()" in the original render()
method with the "response.run()", bypassing the callInThread() call in
the _WSGIResponse's start() method. Even if it works, I hate importing
_WSGIResponse and replicating the code of the original WSGIResource's
render() code. Anyhow, I can't think of a better/more elegant solution
with the current implementation.

I'll appreciate any comment on this.

Thank you.

Cristiano



More information about the Twisted-web mailing list