How to reconnect

Parva
My script gets terminated during the live market hours and prints the following:

ERROR:kiteconnect.ticker:Connection error: 1006 - connection was closed uncleanly (None)
ERROR:kiteconnect.ticker:Connection closed: 1006 - connection was closed uncleanly (None)

I am assuming that it is because of disconnection from the kiteConnect servers. What should I do to solve this Issue?

TIA
  • rakeshr
    @Parva
    You can have look to this thread.If not resolved by above method,paste your WebSocket code here.
  • Parva
    Works Great now. Also, I am trying to calculate candles using a script but if this disconnect and reconnect happens in the middle, I get wrong data in my calculations. Any solution you can think of for this problem?
  • cheko
    I am facing freq disconnect and re connection is also not working. The logs as below

    2019-08-13 11:13:22,003 Connection closed: 1006 - connection was closed uncleanly (WebSocket opening handshake timeout (peer did not finish the opening handshake in time))

    Some times the reconnect works, but some times it just gets struck. How to solve this issue. Below is the kws initialization code.

    # Initialise
    kws = KiteTicker(api_key, access_token,reconnect=True,connect_timeout=60)
  • cheko
    I am facing freq disconnect and re connection is also not working. The logs as below

    2019-08-13 11:13:22,003 Connection closed: 1006 - connection was closed uncleanly (WebSocket opening handshake timeout (peer did not finish the opening handshake in time))

    Some times the reconnect works, but some times it just gets struck. How to solve this issue. Below is the kws initialization code.

    # Initialise
    kws = KiteTicker(api_key, access_token,reconnect=True,connect_timeout=60)
  • rakeshr
    @cheko
    Are you blocking on_tick method with any tick manipulation inside on_tick method?
    If you are then,you need to pass it to another thread, check multi threading example here.
  • cheko
    @rakeshr
    Not sure if it is blocking of not , but i am writing the ticks to mysql DB as per below code. insert_ticks is from another .py file which writes to db.

    def on_ticks(ws, ticks):
    # Callback to receive ticks
    insert_ticks(ticks)
    logging.debug("Ticks: {}".format(ticks))
  • ZI4453
    @checko,is mysqlDB local or hosted in server? is there any other IO intensive script running ?
  • sujith
    @cheko,
    You are blocking the thread. The magnitude of the load depends on the number of instruments you are dealing with. If you have more writes to do then your program becomes unavailable for receiving next ticks.
  • rakeshr
    @cheko
    Yeahinsert_ticks method call seems to be blocking on_tick method.Can you create separate thread for that and pass on the tick.You may check example here.
  • cheko
    @ZI4453
    the sql is a Local hosted server.

    @sujith
    i am getting 58 instruments as of now. Just want to understand, will blocking the thread cause the Socket to disconnect?

    @rakeshr Thanks, i will try the threaded method and update how it goes.

    BTW today there were ~30 disconnections, but it managed to reconnect on its own.
  • rakeshr
    @cheko
    i am getting 58 instruments as of now. Just want to understand, will blocking the thread cause the Socket to disconnect?
    Yeah, it will.
  • ZI4453
    @cheko - please try to use dict or df and write them periodically .. this will give you breather for other executions.
    Real time writing along with threading will require cautious approach to flow coding .....
  • cheko
    @rakeshr i tried threaded = true, but it does nothing just 'Print this' is printed. Below is my code.

    from db_connector import insert_ticks
    ....Tokens etc

    def on_ticks(ws, ticks):
    # Callback to receive ticks
    print(ticks)
    insert_ticks(ticks)
    #logging.debug("Ticks: {}".format(ticks))

    def on_connect(ws, response):
    # Callback on successful connect.
    # Subscribe to a list of instrument_tokens (RELIANCE and ACC here).
    ws.subscribe(tokens)

    #ws.set_mode(ws.MODE_LTP, tokens)
    ws.set_mode(ws.MODE_QUOTE, tokens)


    def on_close(ws, code, reason):
    # On connection close stop the main loop
    # Reconnection will not happen after executing `ws.stop()`
    #ws.stop()
    print("closing connection due to error code : %s and reason : %s" %(code,reason))

    # Callback when connection closed with error.
    def on_error(ws, code, reason):
    print("Connection error: {code} - {reason}".format(code=code, reason=reason))
    logging.info("Connection error: {code} - {reason}".format(code=code, reason=reason))


    # Callback when reconnect is on progress
    def on_reconnect(ws, attempts_count):
    print("Reconnecting: {}".format(attempts_count))
    logging.info("Reconnecting: {}".format(attempts_count))

    # Callback when all reconnect failed (exhausted max retries)
    def on_noreconnect(ws):
    logging.info("Reconnect failed.")


    # Assign the callbacks.
    kws.on_ticks = on_ticks
    kws.on_connect = on_connect
    kws.on_close = on_close
    kws.on_reconnect = on_reconnect
    kws.on_error = on_error
    kws.on_noreconnect = on_noreconnect


    # Infinite loop on the main thread. Nothing after this will run.
    # You have to use the pre-defined callbacks to manage subscriptions.
    kws.connect(threaded=True)

    if kws.is_connected():
    logging.info("### connected")
    else:
    print("Print this")
Sign In or Register to comment.