Rebooting the python fleece server by function

I am writing a python / flask application and would like to add server reboot functionality.

I am currently starting the server with the following option

app.run(debug=True)

which leads to the following: every time a code change occurs

* Running on http://127.0.0.1:5000/
* Restarting with reloader

However, in a production environment, I would rather not install debug=True, but be able to restart the application server whenever I need.

I am trying to get two things to work:

  • if reload_needed: reload_server() and
  • If the user clicks the "Server Restart" button on the admin panel, the function should be called reload_server().

However, despite the fact that the server reboots after changing the code, I could not find a function that allows me to do just that.

, flask/werkzeug. , - , , gunicorn/nginx/apache ..

+4
2

, .

, python/flask (XY.py). (Teamcity), python . , XY.py . // XY.py .

, oneliner os.execl(sys.executable, *([sys.executable]+sys.argv)) , , /, .

: . , - .

global some_queue = None

@app.route('/restart')
def restart():
   try:
     some_queue.put("something")
     return "Quit"

def start_flaskapp(queue):
   some_queue = queue
   app.run(your_parameters)

:

q = Queue()
p = Process(target=start_flaskapp, args=[q,])
p.start()
while True: #wathing queue, sleep if there is no call, otherwise break
   if q.empty(): 
        time.sleep(1)
   else:
      break
p.terminate() #terminate flaskapp and then restart the app on subprocess
args = [sys.executable] + [sys.argv[0]]
subprocess.call(args)

, , !

+2

Source: https://habr.com/ru/post/1569557/


All Articles