From 25c548344b174a824506de96c7aad01a6281ba5d Mon Sep 17 00:00:00 2001 From: Andreas Wrede Date: Sun, 16 May 2021 22:02:54 -0400 Subject: [PATCH] only one event loop --- hbd | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/hbd b/hbd index d47368c..625b0e5 100755 --- a/hbd +++ b/hbd @@ -822,9 +822,12 @@ async def ws_serve(websocket, path): print(f'ws closed: {e}') break print(f"< {name} at {path}") - for h in hbdclass.Host.hosts: + for h in sorted(hbdclass.Host.hosts): jmsg = json.dumps({'type': 'host', 'data': hbdclass.Host.hosts[h].stateinfo() }) await websocket.send(jmsg) + for m in msgs[len(msgs)-20:]: + jmsg = json.dumps({'type': 'message', 'data': m }) + await websocket.send(jmsg) del ws_connections[websocket] @@ -835,7 +838,11 @@ def websocketupdater(): def msg_to_websockets(typ: str, msg: str): jmsg = json.dumps({'type': typ, 'data': msg}) for ws in ws_connections: - asyncio.run(ws.send(jmsg)) + try: + asyncio.run(ws.send(jmsg)) + except Exception as e: + print("ws.send exception: " % e) + exit(1) # # Main @@ -1069,8 +1076,9 @@ except: sys.exit(1) +loop = asyncio.get_event_loop() ws_start_server = websockets.serve(ws_serve, hbd_host, WSPORT) -asyncio.get_event_loop().run_until_complete(ws_start_server) +loop.run_until_complete(ws_start_server) servthread = threading.Thread(target=serv.serve_forever) servthread.daemon = True @@ -1081,7 +1089,6 @@ dnsT = threading.Thread(target=dnsupdatethread) dnsT.daemon = True dnsT.start() -loop = asyncio.get_event_loop() wsT = threading.Thread(target=websocketupdater) wsT.daemon = True wsT.start()