From f03a3d9377ffdda0724133cf01f2ceaefdcd5ca6 Mon Sep 17 00:00:00 2001 From: Matthew Stratford Date: Sun, 18 Apr 2021 21:32:31 +0100 Subject: [PATCH] Make web server restart without hanging. --- web_server.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/web_server.py b/web_server.py index d1b60b4..6df7449 100644 --- a/web_server.py +++ b/web_server.py @@ -5,6 +5,7 @@ from sanic.response import html, text, file, redirect from sanic.response import json as resp_json from sanic_cors import CORS from syncer import sync +import asyncio from jinja2 import Environment, FileSystemLoader from urllib.parse import unquote @@ -349,7 +350,7 @@ def restart(request): # Don't use reloader, it causes Nested Processes! def WebServer(player_to: List[Queue], player_from: List[Queue], state: StateManager): - global player_to_q, player_from_q, server_state, api + global player_to_q, player_from_q, server_state, api, app player_to_q = player_to player_from_q = player_from server_state = state @@ -370,6 +371,11 @@ def WebServer(player_to: List[Queue], player_from: List[Queue], state: StateMana debug=True, auto_reload=False )) - sleep(1) except Exception: break + loop = asyncio.get_event_loop() + if loop: + loop.close() + if app: + app.stop() + del app