only one event loop

This commit is contained in:
2021-05-16 22:02:54 -04:00
parent 9a598d1de1
commit 25c548344b
+10 -3
View File
@@ -822,9 +822,12 @@ async def ws_serve(websocket, path):
print(f'ws closed: {e}') print(f'ws closed: {e}')
break break
print(f"< {name} at {path}") 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() }) jmsg = json.dumps({'type': 'host', 'data': hbdclass.Host.hosts[h].stateinfo() })
await websocket.send(jmsg) 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] del ws_connections[websocket]
@@ -835,7 +838,11 @@ def websocketupdater():
def msg_to_websockets(typ: str, msg: str): def msg_to_websockets(typ: str, msg: str):
jmsg = json.dumps({'type': typ, 'data': msg}) jmsg = json.dumps({'type': typ, 'data': msg})
for ws in ws_connections: for ws in ws_connections:
try:
asyncio.run(ws.send(jmsg)) asyncio.run(ws.send(jmsg))
except Exception as e:
print("ws.send exception: " % e)
exit(1)
# #
# Main # Main
@@ -1069,8 +1076,9 @@ except:
sys.exit(1) sys.exit(1)
loop = asyncio.get_event_loop()
ws_start_server = websockets.serve(ws_serve, hbd_host, WSPORT) 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 = threading.Thread(target=serv.serve_forever)
servthread.daemon = True servthread.daemon = True
@@ -1081,7 +1089,6 @@ dnsT = threading.Thread(target=dnsupdatethread)
dnsT.daemon = True dnsT.daemon = True
dnsT.start() dnsT.start()
loop = asyncio.get_event_loop()
wsT = threading.Thread(target=websocketupdater) wsT = threading.Thread(target=websocketupdater)
wsT.daemon = True wsT.daemon = True
wsT.start() wsT.start()