How I can Reconnect websocket automatically while script is running ?
I tried but it is not working like this,
async def on_close(instance):
logging.warning(“Connection has been closed. Attempting to reconnect…”)
await reconnect(instance)
Function to handle reconnection logic
async def reconnect(instance):
while not shutdown_event.is_set():
try:
await asyncio.sleep(5) # Wait 5 seconds before attempting to reconnect
await instance.connect()
logging.info(“Reconnected to websocket”)
break
except Exception as e:
logging.error(f"Reconnection attempt failed: {e}")
continue