Search code examples
pythonwait

Python - If nothing happens for 1 minute, proceed code


I am writing a script which sends a serial message over a websocket to a device. When I want to start the device I write:

def start(ws):
    """
    Function to send the start command
    """
    print("start")
    command = dict()
    command["commandId"] = 601
    command["id"] = 54321
    command["params"] = {}
    send_command(ws, command)

Every 5 hours or so the device restarts, during the restart, my function start request does not run and my code stops completely.

My question is, is there a way to tell python: "If nothing has happened for 1 minute, try again"


Solution

  • It's not clear exactly what ws is or how you set it up; but you want to add a timeout to the socket.

    https://websockets.readthedocs.io/en/stable/api.html#websockets.client.connect has a timeout keyword; refer to the documentation for details about what it does.

    If this is not the websocket library you are using, please update your question with details.