Jelajahi Sumber

fix session clean task not start in aiohttp and fastapi backend

wangweimin 2 tahun lalu
induk
melakukan
156718079f
2 mengubah file dengan 3 tambahan dan 7 penghapusan
  1. 1 6
      pywebio/platform/aiohttp.py
  2. 2 1
      pywebio/platform/fastapi.py

+ 1 - 6
pywebio/platform/aiohttp.py

@@ -78,10 +78,10 @@ def _webio_handler(applications, cdn, websocket_settings, reconnect_timeout=0, c
     :return: aiohttp Request Handler
     """
     ws_adaptor.set_expire_second(reconnect_timeout)
-    asyncio.get_event_loop().create_task(ws_adaptor.session_clean_task())
 
     async def wshandle(request: web.Request):
         ioloop = asyncio.get_event_loop()
+        asyncio.get_event_loop().create_task(ws_adaptor.session_clean_task())
 
         origin = request.headers.get('origin')
         if origin and not check_origin_func(origin=origin, host=request.host):
@@ -159,14 +159,9 @@ def static_routes(prefix='/'):
     :param str prefix: The URL path of static file hosting, the default is the root path ``/``
     :return: aiohttp routes list
     """
-
-    async def index(request):
-        return web.FileResponse(os.path.join(STATIC_PATH, 'index.html'))
-
     files = [os.path.join(STATIC_PATH, d) for d in os.listdir(STATIC_PATH)]
     dirs = filter(os.path.isdir, files)
     routes = [web.static(prefix + os.path.basename(d), d) for d in dirs]
-    routes.append(web.get(prefix, index))
     return routes
 
 

+ 2 - 1
pywebio/platform/fastapi.py

@@ -59,7 +59,6 @@ def _webio_routes(applications, cdn, check_origin_func, reconnect_timeout):
     """
 
     ws_adaptor.set_expire_second(reconnect_timeout)
-    asyncio.get_event_loop().create_task(ws_adaptor.session_clean_task())
 
     async def http_endpoint(request: Request):
         origin = request.headers.get('origin')
@@ -78,6 +77,8 @@ def _webio_routes(applications, cdn, check_origin_func, reconnect_timeout):
 
     async def websocket_endpoint(websocket: WebSocket):
         ioloop = asyncio.get_event_loop()
+        asyncio.get_event_loop().create_task(ws_adaptor.session_clean_task())
+
         await websocket.accept()
 
         app_name = websocket.query_params.get('app', 'index')