how to solve this error: twisted.internet.error.ReactorNotRestartable:

I was facing the problem of frequent socket timeout. to solve it, Inside on_close() function, I am restarting the main function again but it triggers a new error.
Any idea how to solve it?
  • rakeshr
    No, need to restart socket connection again from on_close.You can just remove call back for on_close and socket will be re-connected.
    Also, frequent disconnection with time-out shouldn't happen, can you paste your complete WebSocket code here?
  • saurabh3679
    @rakeshr I am using flask-script. I start the script like this: python live <stock code>

    This is the live function inside
    def live(name):
    global token
    token = int(name)
    kws = KiteTicker(api_key, access_token, reconnect_max_tries=5, reconnect_max_delay=5, connect_timeout=5)
    # Assign the callbacks.
    kws.on_ticks = on_ticks
    kws.on_connect = on_connect
    kws.on_close = on_close
    Once connected, on_ticks() function works. Here is the on_tick() function. It passes the dataframe to a thread function my_algo where I do the calculations on the dataframe and detect if I need to place the order or not
    def on_ticks(ws, ticks):
    global df_cols, tick_cols, data_frame, readyForOrder
    for company_data in ticks:
    ltp = float(company_data['last_price'])
    timestamp = company_data['timestamp']
    lowest_sell = float(company_data['depth']['sell'][0]['price'])
    highest_buy = float(company_data['depth']['buy'][0]['price'])
    data = [timestamp, ltp, lowest_sell, highest_buy]
    tick_df = pd.DataFrame([data], columns=tick_cols)
    data_frame = pd.concat([data_frame, tick_df], axis=0, sort=True, ignore_index='true')
    data_frame = data_frame.tail(1600)
    if readyForOrder:
    readyForOrder = False
    forwarding_df = data_frame.copy()
    order_thread = threading.Thread(target=my_algo, name='thread1', args=(forwarding_df, ltp))
    This is the on_connect() function in my script
    def on_connect(ws, response):
    global token
    ws.set_mode(ws.MODE_FULL, [token])"Connected to the kiteconnect server. Server response {}".format(response))
    This is the on_close() function in my script
    def on_close(ws, code, reason):
    global readyForOrder
    logger.critical("Server closed the connection!")
    readyForOrder = True
    logger.critical("********** Reconnecting to Websocket*************")
    kws2 = KiteTicker(api_key, access_token, reconnect_max_tries=5, reconnect_max_delay=5, connect_timeout=5)
    # Assign the callbacks.
    kws2.on_ticks = on_ticks
    kws2.on_connect = on_connect
    kws2.on_close = on_close
  • saurabh3679
    Instead of calling the live function now, I moved the websocket instructions inside on_close()
    Will test it today when the market opens
  • saurabh3679
    It triggers the same error. I am not able to connect websocket from on_close().
    any suggestions what to do @rakeshr
  • rakeshr
    Any unhandled exception in your code will result in on_error which in turn calls on_close. If you need the reconnection logic to kick in, just don't use ws.stop() inside on_close. So, not need of Ticker assignment and method callbacks again inside on_close method.You can just remove it.
  • sunpy123
    Hi All,
    Please share the correct sample ticker example as given links are not working

  • rakeshr
    You can check sample ticker example here.
Sign In or Register to comment.