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.

4
  • 3
    You might be interested in this question Commented Dec 30, 2015 at 13:27
  • Thanks @CoryKramer, that is exactly the kind of information I'm looking for. Brilliant, this might be easier to do than I initially imagined :)
    – tsvenson
    Commented Dec 30, 2015 at 13:32
  • 1
    I believe flask and tornado both have features like this, so you could take a look at their code as well.
    – dano
    Commented Jan 5, 2016 at 16:24
  • 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.
    – tsvenson
    Commented Jan 5, 2016 at 21:46

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.