From b50db88273d94a3c7b8dea1f080ba9b50800bce4 Mon Sep 17 00:00:00 2001 From: tt2468 Date: Thu, 8 Jan 2026 11:04:35 -0800 Subject: [PATCH] simplehttp: Simplify simplehttp, updating to newer python standards There's actually no need to run the _run_app() function using a loop we create. Just add an on_startup hook to override the thread pool. Python asyncio automatically shuts down our custom thread pool executor on loop shutdown, so there's no need for any shutdown logic either. asyncio.to_thread will automatically use the running loop's thread pool executor, which is the one we've created ourselves. --- simplehttp.py | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/simplehttp.py b/simplehttp.py index a168fec..de48890 100755 --- a/simplehttp.py +++ b/simplehttp.py @@ -2,16 +2,11 @@ import logging import argparse -from concurrent import futures +import concurrent.futures import asyncio from aiohttp import web -import json import loganalyzer as analyze -loop = asyncio.get_event_loop() -threadPool = futures.ThreadPoolExecutor(thread_name_prefix='loganalyzer: worker thread') -app = web.Application() - with open("templates/index.html", "r") as f: # Grab main HTML page htmlTemplate = f.read() @@ -172,29 +167,29 @@ def sync_request_handler(request): async def request_handler(request): """Async request handler. Submits the incoming request to the thread pool to be handled.""" - return (await loop.run_in_executor(None, sync_request_handler, request)) # Submits the request to a handler inside the threadpool + return await asyncio.to_thread(sync_request_handler, request) # Submits the request to a handler inside the threadpool + + +async def on_startup(app): + threadPool = concurrent.futures.ThreadPoolExecutor(thread_name_prefix='loganalyzer: worker thread') + loop = asyncio.get_running_loop() + loop.set_default_executor(threadPool) # Set the default executor to our thread pool def main(): logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] [%(funcName)s] %(message)s") aiohttpLogger = logging.getLogger('aiohttp') aiohttpLogger.setLevel(logging.WARNING) + parser = argparse.ArgumentParser() parser.add_argument("--host", default="localhost", type=str, help="address to bind to", dest='host') parser.add_argument("--port", default="8080", type=int, help="port to bind to", dest='port') flags = parser.parse_args() - loop.set_default_executor(threadPool) # Set the default executor to our thread pool - app.add_routes([web.get('/', request_handler)]) - applicationTask = loop.create_task(web._run_app(app, host=flags.host, port=flags.port, print=logging.info)) - try: - loop.run_forever() - except KeyboardInterrupt: - pass - finally: - logging.info('Exiting application.') - applicationTask.cancel() # Shuts down the HTTP server - threadPool.shutdown() # Shuts down the running thread pool + app = web.Application() + app.on_startup.append(on_startup) + app.router.add_get('/', request_handler) + web.run_app(app, host=flags.host, port=flags.port) if __name__ == '__main__':