only one event loop
This commit is contained in:
@@ -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:
|
||||
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()
|
||||
|
||||
Reference in New Issue
Block a user