From 41cda9abd7e62166dacc20d939388da8e9c9add5 Mon Sep 17 00:00:00 2001 From: Benjamin Bertrand <benjamin.bertrand@esss.se> Date: Fri, 26 Apr 2019 23:43:26 +0200 Subject: [PATCH] Add SIGTERM handling Properly stop the server when receiving a SIGTERM --- PythonServer/pos-python-server.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/PythonServer/pos-python-server.py b/PythonServer/pos-python-server.py index 42c6e51..a5b5af5 100755 --- a/PythonServer/pos-python-server.py +++ b/PythonServer/pos-python-server.py @@ -2,6 +2,7 @@ import epics import time import json import os +import signal import matplotlib.pyplot as plt from io import BytesIO from git import Repo @@ -484,6 +485,14 @@ class myHandler(BaseHTTPRequestHandler): self.wfile.write(bytes('The server is running', "utf-8")) return + +def on_exit(signum, frame): + print(f'Signal handler called with signal {signum}') + raise SystemExit("Exiting") + + +signal.signal(signal.SIGTERM, on_exit) + try: epicsThread = epicsQuery(stop_signal) epicsThread.start() @@ -491,7 +500,7 @@ try: print ('Started httpserver on port ' , PORT_NUMBER) server.serve_forever() -except KeyboardInterrupt: +except (KeyboardInterrupt, SystemExit): print ('\n^C received, shutting down the web server') server.socket.close() stop_signal.set() -- GitLab