Websocket: Stop ws.run_forever after 15 minutes | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

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()

1st Mar 2022, 9:23 AM
Amirtha K
Amirtha K - avatar
1 Answer
0
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 :)
1st Apr 2022, 5:29 PM
Alan Barrett
Alan Barrett - avatar