Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write python code that can be self-updated without need to quit application?

It is easy to create highly integrated code in python, so the need to quit and restart an application when its code is changed is understandable.

However, it surely must exist some strategies and models to be able to isolate parts of the code so it can be updated on-the-fly without the need to quit and restart.

For the applications I am working on many features will be independant background tasks that the main application will talk to, present status information and also instruct to perform tasks based on the current state. In many ways these background tasks can be seen as independent programs, just that they share some of the codebase with the main application and other tools, tasks, etc built on-top it.

While it probably is hard to make the whole shebang live updateable, I'm sure there must be ways where it is possible to roll out updates and have the running code to notice and update itself as needed.

Since I'm also keen on taking advantage of multithreading and asyncio (in Python 3.5), as well as explore making things stateless, it seems logically possible to do some interesting stuff here that at least makes it possible to avoid some forced hard restarts when rolling out new code.

Would be very grateful for tips and pointers to information about how to make this working.

like image 578
tsvenson Avatar asked Dec 30 '15 13:12

tsvenson


1 Answers

There's a built-in function reload() that will reload a module, but it's easy to mess up. You'd have to be very careful about what holds references to objects created by the old version of the module, and then be sure to replace those when you reload the module.

I use Django a lot, and when it's running its web server in debug mode, it reloads the entire web server process whenever a source file changes. It's a nice compromise between a manual restart and reloading a single module.

I haven't used it, but watchdog might be helpful to monitor the file system for changes to trigger a reload.

like image 71
Don Kirkby Avatar answered Oct 15 '22 05:10

Don Kirkby