Socket connection getting closed frequently

saurabh123
Hi there, i am getting the error --connection was closed uncleanly (WebSocket opening handshake timeout (peer did not finish the opening handshake in time))
continuously. For first few minutes it works well but suddenly it starts giving this error . Due to this i am not getting the current data from websocket frequently.
  • ANL
    1. You may have included heavy computation inside on_ticks; that's why you got error.
    2. Websocket will restart again if you have not included ws.stop() in your code.

    Solution:

    1. Don't put heavy computation or logic inside on_ticks, as it is running a separate thread, kws.connect(threaded=True), and your logic will block this thread. You can assign a separate thread for your calculations and logic.
    2. If you want to continue to stream, don't include ws.stop(), as reconnection will not happen after executingws.stop()
  • saurabh123
    @ANL i have
    def on_ticks(ws, ticks):
    for x in ticks:
    ltp[x['instrument_token']] = x['last_price']

    and when this error 1006 started i also printed the keys of ltp those were only 15 [260105, 3484417, 5582849, 4774913, 4701441, 4267265, 2952193, 2883073, 2748929, 2585345, 1790465, 1895937, 1723649, 1629185, 1270529] can this cause heavy computation ?
  • ANL
    I can't respond without seeing your code.
Sign In or Register to comment.