Python Websocket不会永远运行

时间:2020-02-13 15:56:56

标签: python

即使我使用 run_forever ,我的Python Websocket .py文件也会在关闭10天后自行关闭。

def on_message(ws, message):
    msg = json.loads(message)
    #do stuff here

def on_error(ws, error):
    print(error)
    f = open("websocket_error.txt", "a") #log error
    f.write(error)
    time.sleep(1)
    run(endpoint) #re-run after 1 sec

def on_close(ws):
    print("### closed. Reconnect will be attempted in 1 second ###")
    ws.close()

def on_open(ws):
    print("### open ###")

def run(endpoint):
    websocket.enableTrace(True)
    ws = websocket.WebSocketApp(endpoint,
                                on_open = on_open,
                                on_message = on_message,
                                on_error = on_error,
                                on_close = on_close)

    ws.run_forever()

if __name__ == "__main__":command = 'subscribe=trade:XBTUSD'
    endpoint = 'wss://www.bitmex.com/realtime?'+command

    run(endpoint)

我尝试查看突然关闭是否是由错误引起的,但是日志中没有任何内容。有没有类似的经历?

0 个答案:

没有答案