Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build Twisted servers which are able to do hot code swap in Python?

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.

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:

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

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?

Thanks.

like image 215
Fang-Pen Lin Avatar asked Feb 01 '11 15:02

Fang-Pen Lin


1 Answers

I gave a talk about this at PyCon 2004. There's also some effort to add more functionality to help with this to Twisted itself.

like image 196
Jean-Paul Calderone Avatar answered Oct 11 '22 16:10

Jean-Paul Calderone