Receiving duplicate ticks after reconnect

tapanjbhatt
tapanjbhatt edited April 2022 in Python client
Hello,
I am facing a strange issue for which I could not find any solution on FAQ or any other thread.

I am testing re-connect workflow in case of network issue. To test it, I just disconnect the network cable to mock network issue after ticker is connected.

I have implemented standard logic of re-connect as you have given in your threaded ticker:

while True:
count += 1
time.sleep(1)
if not kws.is_connected():
print('TOACT: kite ticker was disconnected')
print('attempting to reconnect...')
kws.connect(threaded=True)

After i disconnect the network cable, this is what I see in logs, which is expected:

TOACT: kite ticker was disconnected
attempting to reconnect...
TOACT: kite ticker was disconnected
attempting to reconnect...
TOACT: kite ticker was disconnected
attempting to reconnect...
TOACT: kite ticker was disconnected
attempting to reconnect...
TOACT: kite ticker was disconnected
attempting to reconnect...
TOACT: kite ticker was disconnected
attempting to reconnect...
TOACT: kite ticker was disconnected
attempting to reconnect...
TOACT: kite ticker was disconnected
attempting to reconnect...

However, when I reconnect the network cable, strange thing happens:

1. On_connect gets called 8 times. This is same number of times as number of reconnect attempts. See below the print(subscriber_list) is printed 8 times.

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

for instrument_token in fno_trading_symbols.keys():

subscribe_list.append(int(instrument_token))

print(subscribe_list)
ws.subscribe(subscribe_list)

# Set RELIANCE to tick in `full` mode.
ws.set_mode(ws.MODE_FULL, subscribe_list)


[256265, 260105]
[256265, 260105]
[256265, 260105]
[256265, 260105]
[256265, 260105]
[256265, 260105]
[256265, 260105]
[256265, 260105]

2. all subsequent ticks are received 8 times after reconnect:

arrived 256265 : 2022-04-27 17:59:26 : 17038.4
arrived 260105 : 2022-04-27 17:59:08 : 36028.85
--------------------------------------------------------------------------------------------
arrived 256265 : 2022-04-27 17:59:26 : 17038.4
arrived 260105 : 2022-04-27 17:59:08 : 36028.85
--------------------------------------------------------------------------------------------
arrived 256265 : 2022-04-27 17:59:26 : 17038.4
arrived 260105 : 2022-04-27 17:59:08 : 36028.85
--------------------------------------------------------------------------------------------
arrived 256265 : 2022-04-27 17:59:26 : 17038.4
arrived 260105 : 2022-04-27 17:59:08 : 36028.85
--------------------------------------------------------------------------------------------
arrived 260105 : 2022-04-27 17:59:08 : 36028.85
arrived 256265 : 2022-04-27 17:59:26 : 17038.4
--------------------------------------------------------------------------------------------
arrived 260105 : 2022-04-27 17:59:08 : 36028.85
arrived 256265 : 2022-04-27 17:59:26 : 17038.4
--------------------------------------------------------------------------------------------
arrived 256265 : 2022-04-27 17:59:26 : 17038.4
arrived 260105 : 2022-04-27 17:59:08 : 36028.85
--------------------------------------------------------------------------------------------
arrived 260105 : 2022-04-27 17:59:08 : 36028.85
arrived 256265 : 2022-04-27 17:59:26 : 17038.4

This is burderning my system. Every tick is received 8 times and later on will be processed 8 times. How to make sure no duplicate tick is received after reconnect?

Another question, what should be code of on_close? should it be:

def on_close(ws, code, reason):
# On connection close stop the event loop.
# Reconnection will not happen after executing `ws.stop()`
# kite.invalidate_refresh_token()
# ws.stop()
pass

or should it be:

def on_close(ws, code, reason):
# On connection close stop the event loop.
# Reconnection will not happen after executing `ws.stop()`
kite.invalidate_refresh_token()
ws.stop()
# pass

If i keep the second code, i get error: builtins.TypeError: invalidate_refresh_token() missing 1 required positional argument: 'refresh_token'.

I understand that this forum is not for how to write code, but if someone can give me assistance here it would be great help.
  • SRIJAN
    SRIJAN edited April 2022
    1.This is because you are calling kws.connect multiple times in the while loop. So,on_connect gets called multiple times,and so does on_ticks . Because market is closed,you are receiving the last cached tick everytime, making you think that those are duplicate ticks. If it was live market,you would receive live ticks.

    2. You don't need to handle reconnection at your end. pykiteconnect does it for you automatically. :)
    https://github.com/zerodha/pykiteconnect/blob/fc3ee234633a438f5c3d425209322e20dcc6bfca/kiteconnect/ticker.py#L341

    Also,the purpose of threaded ticker is to provide a solution for processing of ticks without the blocking of main on_ticks thread otherwise websocket can disconnect. The same is explained in the example on github. Threaded ticker is not to handle disconnections due to network issues.

    3. Refresh token is not for normal users.
    You can read here:
    https://kite.trade/docs/connect/v3/user/#response-attributes

    4. So, you can just use a pass statement in on_close function. Because if you use ws.stop(), reconnection can't happen.
  • tapanjbhatt
    got you..thanks @srijan....i have fixed this issue as per your suggestion...
This discussion has been closed.