tags:

views:

110

answers:

2

I've written a specialized JSON-RPC server and just started working my way up into the application logic and finding it is a tad annoying to constantly having to stop/restart the server to make certain changes.

Previously I had a handler that ran in intervals to compare module modified time stamps with the past check then reload the module as needed. Unfortunately I don't trust it to work correctly now.

Is there a way for a reactor to stop and restart itself in a manner similar to Paster's Reloadable HTTPServer?

+1  A: 

You could write something similar to paster's reloader, that would work like this:

  1. start your main function, and before importing / using any twisted code, fork/spawn a subprocess.
  2. In the subprocess, run your twisted application.
  3. In the main process, run your code which checks for changed files. If code has changed, reload the subprocess.

However, the issue here is that unlike a development webserver, most twisted apps have a lot more state and just flat out killing / restarting the process is a bad idea, you may lose some state.

There is a way to do it cleanly:

When you spawn the twisted app, use subprocess.Popen() or similar, to get stdin/stdout pipes. Now in your subprocess, use the twisted reactor to listen on stdin (there is code for this in twisted, see twisted.internet.stdio which allows you to have a Protocol which talks to a stdio transport, in the usual twisted non-blocking manner).

Finally, when you decide it's time to reload, write something to the stdin of the subprocess telling it to shutdown. Now your twisted code can respond and shut down gracefully. Once it's cleanly quit, your master process can just spawn it again.

(Alternately you can use signals to achieve this, but this may not be OS portable)

Crast
I am thinking your plan might be the simplest path I can take.
David
+2  A: 

Shipped with Twisted is the twisted.python.rebuild module, so that is probably a good place to start.

Also see this SO question: http://stackoverflow.com/questions/126787/checking-for-code-changes-in-all-imported-python-modules

clemesha
It's not perfect but twisted.python.rebuild seems to be working for my situation.
David