summaryrefslogtreecommitdiff
path: root/web.py
diff options
context:
space:
mode:
authorKeuin <[email protected]>2023-06-03 15:57:56 +0800
committerKeuin <[email protected]>2023-06-03 15:57:56 +0800
commitd845692816079cf78ded919bc1175647576d8529 (patch)
tree603a507d4def49b8d1dfe1d5ca295a3c51803bb0 /web.py
parent7e43c9320a7eb15a60bee04ab41df053cd8f1dc7 (diff)
Use non-blocking file/subprocess IO
Diffstat (limited to 'web.py')
-rw-r--r--web.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/web.py b/web.py
index e2d7fdf..82126b7 100644
--- a/web.py
+++ b/web.py
@@ -1,7 +1,11 @@
+import asyncio
+import os
import re
+import uvicorn
from fastapi import FastAPI, Response
from fastapi.responses import FileResponse
+from uvicorn.loops.auto import auto_loop_setup
import htmlcache
import texgen
@@ -32,3 +36,28 @@ async def get_utaten_lyric_tex(item_id: str):
return Response(content=tex, media_type='application/x-tex')
except htmlcache.TexSourceGenerationError as e:
return Response(content=str(e), status_code=503)
+
+
[email protected]_event("startup")
+async def startup_event():
+ pass
+
+
+def setup_loop():
+ if os.name == 'nt':
+ # use ProactorEventLoop to support async subprocess on Windows
+ print('Driving event loop with IOCP.')
+ loop = asyncio.ProactorEventLoop()
+ asyncio.set_event_loop(loop)
+ auto_loop_setup()
+
+
+if __name__ == '__main__':
+ setup_loop()
+ uvicorn.run(
+ 'web:app',
+ host='127.0.0.1',
+ port=8000,
+ log_level='info',
+ loop='none', # use custom loop initializer
+ )