使 Twisted 应用程序的某些部分可重新加载的策略或支持?

发布于 2024-08-17 12:59:39 字数 228 浏览 3 评论 0原文

我已经编写了一个专门的 JSON-RPC 服务器,刚刚开始研究应用程序逻辑,发现经常需要停止/重新启动服务器来进行某些更改有点烦人。

以前,我有一个处理程序,它每隔一段时间运行一次,将模块修改的时间戳与过去的检查进行比较,然后根据需要重新加载模块。不幸的是,我现在不相信它能正常工作。

有没有一种方法可以让反应器以类似于 Paster 的 Reloadable HTTPServer 的方式自行停止和重新启动?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尛丟丟 2024-08-24 12:59:39

Twisted 附带的是 twisted.python.rebuild 模块,所以这可能是一个很好的起点。

另请参阅这个问题: Checking for codechanges in allimported python模块

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

夏了南城 2024-08-24 12:59:39

您可以编写类似于 Paster 的重新加载器的代码,其工作方式如下:

  1. 启动主函数,并在导入/使用任何扭曲代码之前,分叉/生成一个子进程。
  2. 在子进程中,运行您的扭曲应用程序。
  3. 在主进程中,运行检查更改的文件的代码。如果代码已更改,请重新加载子流程。

然而,这里的问题是,与开发网络服务器不同,大多数扭曲的应用程序都有更多的状态,并且直接终止/重新启动进程是一个坏主意,您可能会丢失一些状态。

有一种方法可以干净地做到这一点:

当您生成扭曲的应用程序时,使用 subprocess.Popen() 或类似的方法来获取 stdin/stdout 管道。现在在您的子进程中,使用twistedreactor来监听stdin(twisted中有相关代码,请参阅twisted.internet.stdio,它允许您拥有一个与stdio传输对话的协议,在通常的扭曲非阻塞方式)。

最后,当您决定重新加载时,向子进程的标准输入写入一些内容,告诉它关闭。现在你扭曲的代码可以响应并优雅地关闭。一旦它完全退出,您的主进程就可以再次生成它。

(或者,您可以使用信号来实现此目的,但这可能不是操作系统可移植的)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文