[Web-SIG] A 'shutdown' function in WSGI

Tarek Ziadé ziade.tarek at gmail.com
Mon Feb 20 20:19:44 CET 2012


On Mon, Feb 20, 2012 at 8:09 PM, Eric Larson <eric at ionrock.org> wrote:

> CherryPy provides a bus that allows you to add events to the web server
> process. It is specified pretty clearly and CherryPy recently made it
> available as a standalone package, Magicbus (
> https://bitbucket.org/cherrypy/magicbus/overview). Specifically it allows
> you to send events on different signals the main server process might get.
> You can also use it for a general bus within the app server, but at its
> most basic level, the goal was to make the stop/start/restart events easy
> to hook into.
>
> I've found it to be really helpful for managing processes and wrote a
> simple supervisor-ish app called Dad using it (
> http://bitbucket.org/elarson/dad).
>

Thanks for the pointer -- that looks pretty neat

I would be more interested though, in defining an extension to the WSGI
standard

A rough example of what I am talking about:

If I take the wsgiref doc, here's an example of a minimal wsgi application
(myapp.py):

from wsgiref.simple_server import make_server

def hello_world_app(environ, start_response):
    status = '200 OK' # HTTP Status
    headers = [('Content-type', 'text/plain')]
    start_response(status, headers)
    return ["Hello World"]
def main():
 return make_server('', 8000, hello_world_app)


That module can be run by any web server out there that understands WSGI.
For instance, with gunicorn I can do:

$ gunicorn myapp:main

What I am talking about is a second entry point for the shutdown - example:

from wsgiref.simple_server import make_server

def hello_world_app(environ, start_response):
    status = '200 OK' # HTTP Status
    headers = [('Content-type', 'text/plain')]
    start_response(status, headers)
    return ["Hello World"]
def main():
   return make_server('', 8000, hello_world_app)

def shutdown():   # or maybe something else as an argument I don't know
   do_some_cleanup()


 And point the shutdown callable to a web server:

$ gunicorn myapp:main myapp:shutdown

If this is defined in the WSGI standard it means any wsgi web server could
call shutdown() , not only gunicorn

Cheers
Tarek



> HTH
>
> Eric
>
> --
> Eric Larson
>
>
> On Monday, February 20, 2012 at 10:03 AM, Tarek Ziadé wrote:
>
> > Hello
> >
> > I need to be able to call a function when the web application shuts down
> (SIGTERM/SIGINT) -- the use case is to stop a background thread.
> >
> > I am currently using signals because it seems to be the most clean way
> to do this. atexit is much trickier since you don't know when it's going to
> get called and you might try to call objects that were garbage collected
> unless you hack something to keep references alive.
> >
> > But signals are also tricky beasts since you may compete with other code
> that are listening to them. For instance mod_wsgi don't like apps that have
> signal handlers.
> >
> > Anyways, the bottom line is that the cleanest way to do this -- as per
> Chris McDonough idea, would be to introduce in the WSGI protocol a
> "shutdown" function the servers would be obligated to call before exiting.
> >
> > I am not sure yet about its arguments, maybe a signum + frame or simply
> an exit code...
> >
> > But how do you like the idea ? That would solve for me the problem of
> having to deal differently here depending on if I am called with mod_wsgi
> or gunicorn or xxx
> >
> >
> > Cheers
> > Tarek
> >
> > --
> > Tarek Ziadé | http://ziade.org
> > _______________________________________________
> > Web-SIG mailing list
> > Web-SIG at python.org (mailto:Web-SIG at python.org)
> > Web SIG: http://www.python.org/sigs/web-sig
> > Unsubscribe:
> http://mail.python.org/mailman/options/web-sig/eric%40ionrock.org
>
>
>
>


-- 
Tarek Ziadé | http://ziade.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/web-sig/attachments/20120220/4cb3ae78/attachment-0001.html>


More information about the Web-SIG mailing list