开发者

Strategies or support for making parts of a Twisted application reloadable?

开发者 https://www.devze.com 2022-12-16 06:14 出处:网络
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 certa

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.

Pr开发者_如何学Goeviously 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?


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

Also see this SO question: Checking for code changes in all imported python modules


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消