Skip to content

Commit 42e3ec3

Browse files
tt2468RytoEX
authored andcommitted
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.
1 parent d8207c1 commit 42e3ec3

1 file changed

Lines changed: 13 additions & 18 deletions

File tree

simplehttp.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,11 @@
22

33
import logging
44
import argparse
5-
from concurrent import futures
5+
import concurrent.futures
66
import asyncio
77
from aiohttp import web
8-
import json
98
import loganalyzer as analyze
109

11-
loop = asyncio.get_event_loop()
12-
threadPool = futures.ThreadPoolExecutor(thread_name_prefix='loganalyzer: worker thread')
13-
app = web.Application()
14-
1510
with open("templates/index.html", "r") as f: # Grab main HTML page
1611
htmlTemplate = f.read()
1712

@@ -172,29 +167,29 @@ def sync_request_handler(request):
172167

173168
async def request_handler(request):
174169
"""Async request handler. Submits the incoming request to the thread pool to be handled."""
175-
return (await loop.run_in_executor(None, sync_request_handler, request)) # Submits the request to a handler inside the threadpool
170+
return await asyncio.to_thread(sync_request_handler, request) # Submits the request to a handler inside the threadpool
171+
172+
173+
async def on_startup(app):
174+
threadPool = concurrent.futures.ThreadPoolExecutor(thread_name_prefix='loganalyzer: worker thread')
175+
loop = asyncio.get_running_loop()
176+
loop.set_default_executor(threadPool) # Set the default executor to our thread pool
176177

177178

178179
def main():
179180
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] [%(funcName)s] %(message)s")
180181
aiohttpLogger = logging.getLogger('aiohttp')
181182
aiohttpLogger.setLevel(logging.WARNING)
183+
182184
parser = argparse.ArgumentParser()
183185
parser.add_argument("--host", default="localhost", type=str, help="address to bind to", dest='host')
184186
parser.add_argument("--port", default="8080", type=int, help="port to bind to", dest='port')
185187
flags = parser.parse_args()
186188

187-
loop.set_default_executor(threadPool) # Set the default executor to our thread pool
188-
app.add_routes([web.get('/', request_handler)])
189-
applicationTask = loop.create_task(web._run_app(app, host=flags.host, port=flags.port, print=logging.info))
190-
try:
191-
loop.run_forever()
192-
except KeyboardInterrupt:
193-
pass
194-
finally:
195-
logging.info('Exiting application.')
196-
applicationTask.cancel() # Shuts down the HTTP server
197-
threadPool.shutdown() # Shuts down the running thread pool
189+
app = web.Application()
190+
app.on_startup.append(on_startup)
191+
app.router.add_get('/', request_handler)
192+
web.run_app(app, host=flags.host, port=flags.port)
198193

199194

200195
if __name__ == '__main__':

0 commit comments

Comments
 (0)