My HTTP runs using aiohttp.web module:
import asyncio as aio
import aiohttp.web as web
server = web.Application()
server.add_routes([...])
web.run_app(server, port=8080)
The code inside web.run_app
use the main event loop, it handles KeyboardInterrupt
exception and exit when Ctrl+C is pressed, in simple apps. However, I need to terminate all threads, which aiohttp.web won't, and the programme doesn't exit.
How to override the default signal handler of aiohttp.web.Application?
Thanks to @user4815162342 in the comments, 2 solutions for the problem:
Solution 1: Add daemon=True
:
my_thread = Thread(target=..., daemon=True)
Solution 2: Wrap web.run_app
in a try
block:
try:
web.run_app(server,...)
finally:
terminate_threads()