<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On 22 Οκτ 2013, at 3:38 μ.μ., <a href="mailto:exarkun@twistedmatrix.com">exarkun@twistedmatrix.com</a> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">On 07:41 am, <a href="mailto:orestis@orestis.gr">orestis@orestis.gr</a> wrote:<br><blockquote type="cite">On 21 Οκτ 2013, at 10:32 μ.μ., Glyph <<a href="mailto:glyph@twistedmatrix.com">glyph@twistedmatrix.com</a>> wrote:<br><blockquote type="cite"><br>On Oct 20, 2013, at 2:21 AM, Orestis Markou <<a href="mailto:orestis@orestis.gr">orestis@orestis.gr</a>> wrote:<br><blockquote type="cite">Hello,<br><br>Short form of the question:<br><br>Are people using Twisted to host WSGI applications AND serve static files AND replace celery/redis/other?<br></blockquote><br>I'm not personally using it as a WSGI host, but otherwise, yes, a full-stack application container speaking multiple protocols.<br></blockquote><br>Any pointers on how to best use this in combination with WSGI/Django? In the past I had a combination of twisted-web (for /static and /media) and wsgi host (for everything else), all running under the same Service. Essentially:<br><br>os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'<br>application = django.core.handlers.wsgi.WSGIHandler()<br><br>wsgi_resource = WSGIResource(reactor, reactor.getThreadPool(), application)<br>resource = Root(wsgi_resource)<br># this could probably be automatically inferred from settings.py<br>resource.putChild('static', File(...))<br>resource.putChild('uploaded', File(...))<br># other stuff<br>site = server.Site(resource)<br>reactor.listenTCP(8000, site)<br>reactor.run()<br></blockquote><br>This looks about like I'd expect (if my guess that `Root` is an `IResource` that knows how to split dispatch between the WSGI resource and its other children).  If you have any suggestions for improving the experience please share them. :)<br></blockquote><div><br></div><div>For anyone following along:</div><div><br></div><div><div><font face="Courier">class Root(resource.Resource):</font></div><div><font face="Courier"><br></font></div><div><font face="Courier">    def __init__(self, wsgi_resource):</font></div><div><font face="Courier">        resource.Resource.__init__(self)</font></div><div><font face="Courier">        self.wsgi_resource = wsgi_resource</font></div><div><font face="Courier"><br></font></div><div><font face="Courier">    def getChild(self, path, request):</font></div><div><font face="Courier">        path0 = request.prepath.pop(0)</font></div><div><font face="Courier">        request.postpath.insert(0, path0)</font></div><div><font face="Courier">        return self.wsgi_resource</font></div><div><font face="Courier"><br></font></div><div>I don't remember now if I wrote this code or if I copied it from somewhere.</div></div><br><blockquote type="cite"><blockquote type="cite">[snip]<br><br>Here's a thought experiment - I'd like to keep URL routing 100% in Django for anything that hits the DB. I have some code that needs to spawn an external process to generate an image on-demand (with a layer of caching on top). In the past I defined a Twisted.Web handler for that. Could I now expose an internal API that (through Crochet?) do the spawnProcess dance and come back with image data that Django could then handle internally (store in a file, whatever). How would the threaded WSGI container deal with the blocking request (not really blocking, but that request would stall until the Deferred would be fired).<br></blockquote><br>It will produce roughly the same results as you'd get if you used any other WSGI container: one of the threads in the thread pool will be kept unavailable as it waits for the result.  This will have the usual consequence: if your threadpool has a max of N threads and you receive N requests that need to do this, the N+1th request that needs to be handled by the WSGI part of your server won't be handled until one of the previous requests completes (completion frees up one thread which is then used to handle the N+1th request).<br><br>The only difference might be that since you also have non-WSGI content (all of your static content) even when your thread pool is completely in use requests for static content will still be satisfied right away. However, if you previously had a WSGI+lighttpd/whatever then you probably already had this property as well.<br><br>Put another way, Twisted's WSGI container is still just a WSGI container.  Fortunately Twisted has other pieces aside from a WSGI container. :)<br></blockquote><div><br></div><div>I wonder if this could be turned on its head - instead of having the Django WSGI app blocking while waiting for a long running thing, field the request beforehand, do whatever long-running thing is required and only then hand over the to the WSGI app to proceed with the HTTP response. This obviously requires a level request processing in Twisted - I'm not sure if it's worth the effort. It could be done by a Twisted-savvy WSGI middleware, perhaps. I'll try to see what I can come up with.</div><div><br></div><div>Orestis</div><div><br></div><div><br></div><div><br></div><blockquote type="cite">Jean-Paul<br><br>_______________________________________________<br>Twisted-Python mailing list<br><a href="mailto:Twisted-Python@twistedmatrix.com">Twisted-Python@twistedmatrix.com</a><br>http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python<br></blockquote></div><br></body></html>