Search code examples
pythondjangotwistedevent-driventwisted.web

Is twistedweb with django recommeneded


I have a Django application which I need to deploy in a WSGI container. I can either chose an event driven app server like TwistedWeb or a process driven server like uWSGI. I completely understand the difference between an event driven and a process driven server and I know Django framework is blocking in nature.

I came across TwistedWeb which lets us run a WSGI application in a simple fashion.

My questions are as follows:

1) Would I gain anything by running Twisted instead of uWSGI as Django is blocking in nature. Is TwistedWeb different from the standard twisted library ? I know people run Twisted with Django when they need support for async as well, for ex chat along with normal functionality and they still want to have just one app. I have no such use case and for me its just a website.

2) Would the performance be worse on TwistedWeb as its just a single process and my request would block as Django is synchronous in nature ? Or TwistedWeb runs something like uWSGI which launches multiple processes before hand and distributes requests in a roundrobin fashion among those ? If yes then is TwistedWeb any better than uWSGI ?

3) Is there any other protocol other than WSGI which can integrate Twisted with Django and still give me async behavior (trying my luck here :) )


Solution

  • You could have asked each of these as a separate question; any time you feel the need to enumerate a list, you should probably do that. But here goes:

    1. Yes, you would gain something. By hosting your Django application in Twisted, you get access to the Twisted mainloop and thereby all Twisted APIs, which you can access easily with something like Crochet. You also eliminate the overhead of having a separate web server, since Twisted can serve as an external web server as well as an application container.
    2. Performance is probably bottlenecked on your database and your Django code. Twisted is likely to be slightly slower than something like nginx at, for example, serving your static content directly, but you can make that difference fairly minimal by running your twisted server under PyPy. It seems like you're asking about concurrency though - something like "will Twisted's WSGI container run only one Django request at a time?" - to which the answer is "no". Twisted's WSGI container is multi-threaded and will run Django code more or less as it expects to be run.
    3. It's not clear what you're asking here, so perhaps ask that other question after all.