Threads creation optimization

This commit is contained in:
Eden Kirin
2023-03-30 21:13:54 +02:00
parent 059408242c
commit c9707c0523
5 changed files with 17 additions and 14 deletions

View File

@ -17,10 +17,14 @@ from hopper.models.ws_dto import (
WSProductPurchaseDone,
WSProductPurchaseStart,
)
from settings import settings
class WSServer(Thread):
def __init__(self, host: str, port: int) -> None:
self.host = host
self.port = port
super().__init__(daemon=True)
async def handler(self, websocket: WebSocketServerProtocol) -> None:
"""New handler instance spawns for each connected client"""
self.connected_clients.add(websocket)
@ -95,13 +99,13 @@ class WSServer(Thread):
async def run_async(self) -> None:
logging.info(
f"Starting FairHopper Websockets Server on {settings.ws_server.HOST}:{settings.ws_server.PORT}"
f"Starting FairHopper Websockets Server on {self.host}:{self.port}"
)
async with websockets.serve(
ws_handler=self.handler,
host=settings.ws_server.HOST,
port=settings.ws_server.PORT,
host=self.host,
port=self.port,
):
await asyncio.Future() # run forever