Skip to content

Instantly share code, notes, and snippets.

@ianschenck
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ianschenck/315eef1f5de8710743f8 to your computer and use it in GitHub Desktop.
Save ianschenck/315eef1f5de8710743f8 to your computer and use it in GitHub Desktop.
Run twisted wsgi like an old-school, socket sharing fcgi subprocess
# Many very old fcgi containers had a trick probably inherited from old
# inetd, where a socket is syscall dup2'd onto fd 0 of a subprocess.
# The parent creates and binds the socket while the children then just
# listen on it.
#
# Something to note is that supervisord provides this facility with its
# [fcgi-program] configuration section. The benefit here is simple inet
# or fcgi style graceful restarts. Anyhow, here is how you grab a
# socket that is fd 0 and start your twisted wsgi container on it. The
# wsgi application here is `application`.
from twisted.internet import fdesc
from twisted.internet import reactor
from twisted.web.server import Site
from twisted.web.wsgi import WSGIResource
resource = WSGIResource(reactor, reactor.getThreadPool(), application)
site = Site(resource)
fdesc.setNonBlocking(0)
reactor.adoptStreamPort(0, socket.AF_INET, site)
reactor.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment