如何构建能够在Python中进行热代码交换的Twisted服务器? [英] How to build Twisted servers which are able to do hot code swap in Python?

查看:104
本文介绍了如何构建能够在Python中进行热代码交换的Twisted服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了一套音频流服务器,它们全部都使用Twisted,并且它们都是使用Python.它们可以工作,但是问题一直困扰着我,当我在正在运行的服务器中发现一些错误时,或者我想在服务器中添加一些东西时,我需要停止它们并重新启动.与HTTP服务器不同,可以随时重新启动它们,但对于音频流服务器则不行.重新启动流服务器后,这意味着我的用户将断开连接.

I've developed a set of audio streaming server, all of them are using Twisted, and they are in Python, of course. They work, but a problem keeps troubling me, when I found some bugs there in the running server, or I want add something into the server, I need to stop them and start. Unlike HTTP servers, it's okay to restart them whenever, but not okay with audio streaming servers. Once I restart my streaming server, it means my users will encounter a disconnection.

我确实尝试设置一个人孔(用于Twisted服务器的ssh服务,您可以登录并在控制台中键入Python代码以执行某些操作),然后连接到控制台,即时重新加载Python模块.它有时可以工作,但很难控制.您永远不会知道服务器中有多少个旧类实例,并且其中某些实例可能难以访问,并且类之间的关系非常复杂.同样,它可能在某些情况下可行,但是有时您确实需要重新启动服务器,例如,您正在使用选择器反应器运行服务器,而您想改为使用epoll反应器运行服务器,则必须重新启动它.另一个示例,当内存使用率过高时,您也必须重新启动它们.

I did try to setup a manhole (a ssh service for Twisted servers, you can login and type Python code in the console to do something), and connect to the console, reload Python modules on the fly. It works sometimes, but hard to control. You never know how many instances of old class are there in the server, and some of them might be hard to reach, and relationships of class would be very complex. Also, it may works in some situations, but sometimes you really need to restart server, for example, you are running the server with selector reactor, and you want to run it with epoll reactor instead, then you have to restart it. Another example, when the memory usage goes too high, you have to restart them, too.

要构建这样的系统,我脑子里浮现出一个想法,我想这是可能的,可以将这些连接和数据从一个流程移交给另一个流程.例如:

To build such system, I have an idea comes in my head, I'm thinking is that possible to hand over those connections and data from a process to another. For example:

我们有一个名为Broadcasting的服务器,并且正在运行的实例在rev.123下,我们希望将其替换为rev.124.

We have a Server named Broadcasting, and the running instance is under rev.123, and we want replace it with rev.124.

Broadcasting rev.123 is running....
Startup Broadcasting rev.124 ....
Broadcasting rev.124 is stand by
Hand over connections from instance of rev.123 to instance of rev.124
Stop Broadcasting rev. 123 instance

这可能吗?我不知道套接字句柄的生命周期是否绑定到进程,我认为进程创建的套接字在创建者进程被杀死时会关闭,但是我不确定.如果可能,是否有设计此类热代码交换机制的指南或文章?有什么可以实现我想要完成的《 Twisted》的目标吗?

Is this possible? I have no idea that does lifetime of socket handles bound to processes or not, I thought sockets created by a process will be closed when the creator process is killed, but I'm not sure. If it is possible, are there any guidelines or articles for designing such kind of hot code swapping mechanism? And is there something can achieve what I want for Twisted already be done?

谢谢.

推荐答案

还需要努力为Twisted本身添加更多功能,以帮助解决此问题.

这篇关于如何构建能够在Python中进行热代码交换的Twisted服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆