소스 검색

delete clients without socket connection after 60 seconds

Falko Schindler 2 년 전
부모
커밋
cbbb97fd0b
2개의 변경된 파일15개의 추가작업 그리고 1개의 파일을 삭제
  1. 1 1
      nicegui/client.py
  2. 14 0
      nicegui/nicegui.py

+ 1 - 1
nicegui/client.py

@@ -1,6 +1,5 @@
 import asyncio
 import json
-import os
 import time
 import uuid
 from pathlib import Path
@@ -25,6 +24,7 @@ class Client:
 
     def __init__(self, page: 'page', *, shared: bool = False) -> None:
         self.id = str(uuid.uuid4())
+        self.created = time.time()
         globals.clients[self.id] = self
 
         self.elements: Dict[str, Element] = {}

+ 14 - 0
nicegui/nicegui.py

@@ -1,4 +1,5 @@
 import asyncio
+import time
 import urllib.parse
 from pathlib import Path
 from typing import Dict, Optional
@@ -50,6 +51,7 @@ def handle_startup(with_welcome_message: bool = True) -> None:
     create_favicon_routes()
     [safe_invoke(t) for t in globals.startup_handlers]
     create_task(binding.loop())
+    create_task(prune_clients())
     globals.state = globals.State.STARTED
     if with_welcome_message:
         print(f'NiceGUI ready to go on http://{globals.host}:{globals.port}')
@@ -126,3 +128,15 @@ def get_client(sid: str) -> Optional[Client]:
     query = urllib.parse.parse_qs(query_bytes.decode())
     client_id = query['client_id'][0]
     return globals.clients.get(client_id)
+
+
+async def prune_clients() -> None:
+    while True:
+        stale = [
+            id
+            for id, client in globals.clients.items()
+            if not client.shared and not client.has_socket_connection and client.created < time.time() - 60.0
+        ]
+        for id in stale:
+            del globals.clients[id]
+        await asyncio.sleep(10)