Members, good afternoon. Please tell me how to properly shut down the ws-server (aiohttp)? I run on windows and loop.add_signal_handler (signal.SIGTERM, handler, loop) does not work.

I have the following server code: #! / Usr / bin / env python # - - coding: utf-8 - -

import asyncio import signal import aiohttp.web from aiohttp import ClientConnectionError # This restores the default Ctrl+C signal handler, which just kills the process #https://stackoverflow.com/questions/27480967/why-does-the-asyncios-event-loop-suppress-the-keyboardinterrupt-on-windows import signal signal.signal(signal.SIGINT, signal.SIG_DFL) HOST = os.getenv('HOST', '127.0.0.1') PORT = int(os.getenv('PORT', 8881)) async def testhandle(request): #Сопрограмма одрабатывающая http-запрос по адресу "http://127.0.0.1:8881/test" print("server: into testhandle()") return aiohttp.web.Response(text='Test handle') async def websocket_handler(request): #Сопрограмма одрабатывающая ws-запрос по адресу "http://127.0.0.1:8881" print('Websocket connection starting') ws = aiohttp.web.WebSocketResponse() await ws.prepare(request) print('Websocket connection ready') try: async for msg in ws: if msg.type == aiohttp.WSMsgType.TEXT: #print(msg.data) if msg.data == 'close': await ws.close() break else: await ws.send_str("You said: {}".format(msg.data)) elif msg.type == aiohttp.WSMsgType.ERROR: print('ws connection closed with exception %s' % ws.exception()) except (asyncio.CancelledError, ClientConnectionError): pass # Тут оказываемся когда, клиент отвалился. # В будущем можно тут освобождать ресурсы. finally: print('Websocket connection closed') return ws def main(): loop = asyncio.get_event_loop() app = aiohttp.web.Application() app.add_routes([aiohttp.web.get('/', websocket_handler), aiohttp.web.get('/test', testhandle)]) try: aiohttp.web.run_app(app, host=HOST, port=PORT, handle_signals=True) finally: loop.close() if __name__ == '__main__': main() 

And when I send a "cancel" from the client, the server closes the socket. But it does not complete its work and does not give up management. It keeps spinning somewhere in run_app () .....

How to close the server correctly? Thank you all in advance for your answers.

0