[Twisted-web] Shutting down server via web-request with nice status page

Maciej Szumocki mszumocki at contman.pl
Wed Feb 23 06:20:01 MST 2005


Hello, 

 I have question about how does reactor.stop() work with Nevow.
I have a simple web server (in twisted/nevow) that does something in background, and
there's a page that's supposed to close the server gracefully. I would like the request
to be served this way:
 - client sends the request (opens the page in browser)
 - he gets half-finished page with something like "Shutting down..."
 - server does some possibly time-confusing stuff then sends "Finished" completing the page
 - server stops
I'm pretty new to both twisted and nevow, and this is what i've come up so far quickly.
Surprisingly, it works to some extent, as in server does indeed stop, but i'm looking for 
a way to make it non-blocking, so other client who connect in the same time would get
alternative page with "server closing down" message.

class Terminate(rend.Page):
    """
    Rendering this resource will stop the import process if it's running, then save current
    state and configuration and gracefully shutdown import server
    """

    NAME = 'Terminating server'

    docFactory = loaders.stan(
        html[
            head[page_title(NAME)],
            body[
                main_header(NAME, url.root),
                ul(data = directive('execute'), render=directive("sequence"))[
                    li(pattern='item')[span(render=directive("string")), "... "],
                ],
                hr,
                h2['Server stopped'],
                ]
            ]
    )

    def data_execute(self, context, data):
        if work.running():
            yield "Stopping import process"
            work.stop()
        yield "Saving configuration"
        work.save()
        yield "Shutting down"
        work.terminate()
        yield "Done."

work.terminate() simply calls reactor.stop() at the moment. work.stop() and work.save() are the parts
of blocking code - i intentd to rewrite them to either work in thread or by using deferreds (assuming i manage to
figure out how do deferreds work exactly ;) 
Could you please give me some ideas as to how should data_execute method look to achieve effect i want?

Maciej Szumocki




More information about the Twisted-web mailing list