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

1

7

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.

Cohbert answered 30/12, 2015 at 13:23 Comment(4)
You might be interested in this questionEspadrille
Thanks @CoryKramer, that is exactly the kind of information I'm looking for. Brilliant, this might be easier to do than I initially imagined :)Cohbert
I believe flask and tornado both have features like this, so you could take a look at their code as well.Forgot
Thanks @dano, Flask is one of the micro frameworks I'm looking at as candidates for several projects so this makes it even more interesting.Cohbert
B
2

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.

Burl answered 2/2, 2016 at 0:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.