Websocket: Stop ws.run_forever after 15 minutes | Sololearn: Learn to code for FREE!


Websocket: Stop ws.run_forever after 15 minutes

Hi I'm working on this Finnhub api where I get the details of the selected stocks. As i give ws.run_forever(), it is not stopping. But i wanna run it for 15 mins and then stop. Also keep_running= False is not working for me. Any idea guys? import websocket def on_message(ws, message): print(message) def on_error(ws, error): print(error) def on_close(ws): print("### closed ###") def on_open(ws): ws.send('{"type":"subscribe","symbol":"AAPL"}') ws.send('{"type":"subscribe","symbol":"AMZN"}') ws.send('{"type":"subscribe","symbol":"BINANCE:BTCUSDT"}') ws.send('{"type":"subscribe","symbol":"IC MARKETS:1"}') if name == "main": websocket.enableTrace(True) ws = websocket.WebSocketApp("wss://ws.finnhub.io?token=", on_message = on_message, on_error = on_error, on_close = on_close) ws.on_open = on_open ws.run_forever()

3/1/2022 9:23:40 AM

Amirtha K

1 Answer

New Answer


hey, in kinda a novice but try adding this at the point you need the wait! asyncio.sleep(60*15) if sleep dosen't work change it to wait :) oh and you'll need to import asyncio at the top of page :)