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

nawaz1001
Following the documentation from https://kite.trade/docs/pykiteconnect/v3/#kiteconnect.KiteConnect.quote to get MarketQuotes of mutiple instrument_tokens. It running fine for 20-30 secs and after that throwing error:-
ERROR:kiteconnect.ticker:Connection error: 1006 - connection was closed uncleanly (None)
ERROR:kiteconnect.ticker:Connection closed: 1006 - connection was closed uncleanly (None)
I have my code as-
def on_ticks(ws, ticks):
print(ticks)
for tick in ticks:
market_quotes = MarketQuotes.objects.create(instrument_token = tick.get('instrument_token'))
market_quotes.tradable = tick.get('tradable')
market_quotes.instrument_token = tick.get('instrument_token')
market_quotes.last_price = tick.get('last_price')
market_quotes.buy_quantity = tick.get('buy_quantity')
market_quotes.sell_quantity = tick.get('sell_quantity')
market_quotes.timestamp = tick.get('timestamp')
market_quotes.save()

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

ws.subscribe(ins_token_list)
ws.set_mode(ws.MODE_FULL,ins_token_list)

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

def subscription_for_instrument():
# Assign the callbacks.
api_key = 'lqcf'

access_token = get_or_none(KiteToken, is_valid = True)

kws = KiteTicker(api_key, access_token.access_token)
kws.on_ticks = on_ticks
kws.on_connect = on_connect
kws.on_close = on_close

# Infinite loop on the main thread. Nothing after this will run.
# You have to use the pre-defined callbacks to manage subscriptions.
kws.connect()
How to rectify it?
  • sujith
    You can refer to this thread.
  • nawaz1001
    Tried kws.connect(threaded=True)
    Getting same error after some time.
  • tahseen
    tahseen edited March 2020
    def function_to_read_queue():
    # this function is run through a thread
    # Read queue within endless while loop
    # Do all the mess here


    def on_ticks(ws, ticks):
    # Push ticks in a Queue the ticks and that is it.
    # Nothing else unless you don't want socket to disconnect
  • nawaz1001
    @tahseen thanks for suggestion. Resolved it with threading concept.
This discussion has been closed.