websocket errors - not reconnecting

Rishant
Hi,

I am getting following errors:

ERROR:websocket:error from callback >: on_error() takes no arguments (2 given)
ERROR:websocket:error from callback >: on_close() takes no arguments (1 given)

The question is that why is it not trying to reconnect .What is does is that it stops ?
Tagged:
  • sujith
    Hi,
    Can you let us know which client you are using?
  • Rishant
    Rishant edited December 2017
    I am using the latest version of kiteconnect in python . It works mostly fine for most part of the days, except for once or twice this error comes.
  • sujith
    You can use reconnect feature available in pykiteconnect. You can check out example here.
  • Rishant
    Rishant edited December 2017
    I am using the same code . let me share my code with you :
    import cPickle as pickle
    from kiteconnect import WebSocket
    import time
    from datetime import datetime
    from Queue import Queue,LifoQueue
    from threading import Thread
    import pandas as pd
    import MySQLdb
    df = pd.DataFrame()

    q =LifoQueue(maxsize=10)
    #max size is to ensure that the program is not running even after the WebSocket has broken for long
    #tickData = 0

    # Initialise.
    kws = WebSocket("*****", "******", "*****", reconnect=True)

    # Callback for tick reception.
    def on_tick(tick, ws):
    tick.append(datetime.now())
    q.put(tick)
    #tickdata = tick
    #time.sleep(10)

    # Callback for successful connection.
    def on_connect(ws):
    # Subscribe to a list of instrument_tokens (RELIANCE and ACC here).
    listSubscription = [134657, 1629185, 895745, 3924993, 348929, 758529, 359937, 784129, 3001089, 415745, 2796801, 356865, 140033]
    ws.subscribe(listSubscription)

    # Set RELIANCE to tick in `full` mode.
    ws.set_mode(ws.MODE_QUOTE, listSubscription)

    def on_close():
    print("closed")

    def on_error():
    print("error")

    # Assign the callbacks.
    kws.on_tick = on_tick
    kws.on_connect = on_connect
    kws.on_close = on_close
    kws.on_error = on_error

    # Assign the callbacks.
    kws.on_tick = on_tick
    kws.on_connect = on_connect

    kws.enable_reconnect(reconnect_interval=5, reconnect_tries=50)

    # 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)
    test = {}
    data_instrument_token = pd.DataFrame(columns = ['last_price','timeStamp','ratiobuysell'] )
    #data = {738561:{'last_price':[],'timeStamp':[]}}
    #data_instrument_token.set_index('timeStamp')
    ltpDict = {134657:0, 1629185:0, 895745:0, 3924993:0, 348929:0, 758529:0, 359937:0, 784129:0, 3001089:0, 415745:0, 2796801:0, 356865:0, 140033:0}
    test['universe'] ={134657: 'BPCL', 1629185: 'NATIONALUM', 895745: 'TATASTEEL', 3924993: 'NMDC', 348929: 'HINDALCO', 758529: 'SAIL', 359937: 'HINDPETRO', 784129: 'VEDL', 3001089: 'JSWSTEEL', 415745: 'IOC', 2796801: 'GODREJIND', 356865: 'HINDUNILVR', 140033: 'BRITANNIA'}
    1,1 ratiobuysell = 0
    i=0
    #A Pandas Thread
    while True:

    a = q.get()

    for j in range(0,len(a)-1):

    #data_instrument_token.set_value(i,'last_price',a[j]['last_price'])
    #data_instrument_token.set_value(i,'instrument_token',a[j]['instrument_token'])
    #data_instrument_token.set_value(i,'timeStamp',a[len(a)-1])
    if a[j]['sell_quantity'] > 0:
    ratiobuysell = int(float(a[j]['buy_quantity'])*100/a[j]['sell_quantity'])
    print a[len(a)-1]
    args = (a[j]['instrument_token'],a[j]['last_price'],a[len(a)-1], ratiobuysell)
    sql = "INSERT INTO tickData(token,close,date,ratiobuysell)"" VALUES(%s,%s,%s,%s)"
    conn = MySQLdb.connect(host="****", user="****", passwd="****", db="****")
    cursor = conn.cursor()
    dataDb = cursor.execute(sql,args)
    ltpDict[a[j]['instrument_token']]=a[j]['last_price']
    conn.commit()

    query = """ UPDATE stocksData
    SET BPCL = %s,NATIONALUM = %s,TATASTEEL = %s,NMDC = %s,HINDALCO = %s,SAIL = %s,HINDPETRO = %s,VEDL = %s,JSWSTEEL = %s,IOC = %s, GODREJIND = %s,HINDUNILVR = %s,BRITANNIA = %s,date = %s
    WHERE fieldInfo = "LastTradedPrice"; """
    dataLTP = (ltpDict[134657],ltpDict[1629185],ltpDict[895745],ltpDict[3924993],ltpDict[348929],ltpDict[758529],ltpDict[359937],ltpDict[784129],ltpDict[3001089],ltpDict[415745],ltpDict[2796801],ltpDict[356865],ltpDict[140033],a[len(a)-1])
    dataDb = cursor.execute(query,dataLTP)
    conn.commit()
    conn.close()
    i = i + 1
  • sujith
    Can you set on_error callback something like this and print error message?
  • Rishant
    So, now I changed the callback on error function as below:

    def on_error(error, ws):
    print("error")

    Is it ok ?
  • sujith
    Yes, if you can let us know that error then we can investigate the issue.
  • Rishant
    Rishant edited December 2017
    Thanks a lot Sujith, I think the issue will not come again as I think the issue was . I will post my queries in case it comes again.

    as mentioned in the first message

    ERROR:websocket:error from callback >: on_error() takes no arguments (2 given)
    ERROR:websocket:error from callback >: on_close() takes no arguments (1 given)
This discussion has been closed.