2

Question

  • Is There a way to auto-restart a Python Werkzeug server from the code base, dynamically, same of what you can do with Watchdog or nodemon but?

Also important it should not be done from terminal nor from a config file, because these are not reachable.

Objective

I need to programmatically write new peace of code within the server code base which also requires a database migration, hence the server needs to re-start, all of this in run time and while the server is running

Mode Details

Basically, I've a Werkzeug server, similar to Flask which I require to auto re-start, I've found this but gives a solution in a command line:

An instance would be

def do_some_computation(restart_server=False):
    """Sample code"""
    watchdog_obj = object()
    computation = 'SOME_COMPUTATION'
    for c in computation:
        print("computing...")
        print("writing new python code to file x...")
    if restart_server:
        watchdog_obj.restart_server_now()


do_some_computation(restart_server=True)

Then on the server log terminal I would expect

2021-21-21 .....  Initiating shutdown
2021-21-21 .....  Hit CTRL-C again or send a second signal to force the shutdown.
2021-21-21 .....  AutoReload watcher
2021-21-21 .....  HTTP service (werkzeug) ...

Is this possible?

Federico Baù
  • 6,013
  • 5
  • 30
  • 38

0 Answers0