Problem in closing connection webscoket using new from kiteconnect import kiteticker

menaveenn
Hi Team,

Im trying to get the data from websocket using KiteTicker method .
I'm able to get the data but im not able to close the connection,everytime its going in infinite loop.
Below is the code used to close the connection after printing the pay load one time ,it should stop printing the data after first time ,but it keeps running in infinite loop.

Can you suggest me what im doing wrong ?
def on_tick(tick, ws):
print tick
ws.close()
# Callback for successful connection.
def on_connect(ws):
ws.subscribe(tokens)
ws.set_mode(ws.MODE_FULL, tokens)

kws.on_tick = on_tick
kws.on_connect = on_connect
kws.connect(disable_ssl_verification=True) #im using UBUNTU
Regards,
Naveen
Tagged:
  • sujith
    Hi @menaveenn,
    You need to call stop_retry and then close connection.
  • menaveenn
    @sujith

    As you suggested ,I tried with below code .
    def on_ticks(ws, ticks):
    # Callback to receive ticks.
    print(ticks)
    ws.stop_retry()
    ws.close()

    but thread is not closing even after that ,but I can see its not trying to connect again and again.
  • menaveenn
    @vivek any inputs from you would be great on this problem!!
  • menaveenn
    @sujith ,Can you let me know the mistake im doing ?
    Regards,
    Naveen
  • Vivek
    Vivek edited February 2018
    Please update to v3.7.0b8 where I have added a method called `stop` to stop the main thread. Also note that now if you call method `close` you don't have to call `stop_retry` since its implicitly disabled. Here is a modified example
    import logging
    from kiteconnect import KiteTicker

    logging.basicConfig(level=logging.DEBUG)

    # Initialise
    kws = KiteTicker("your_api_key", "your_access_token")

    def on_ticks(ws, ticks):
    # Callback to receive 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([738561, 5633])

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

    def on_close(ws, code, reason):
    # On connection close stop the main loop
    ws.stop()

    # Assign the callbacks.
    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()
  • kbkvadivel
    Hi,

    From above example, please guide me how i can close the connection properly after receiving 1 tick? some example please..
  • sujith
    @kbkvadivel,
    You need to call close() method. You can check out python documentation.
    Inside on_close() you need to call stop as mentioned by @vivek.
  • pracas
    pracas edited June 2018
    @sujith / @vivek when i try to close the connection like this, i get the following error

    builtins.AttributeError: 'KiteTicker' object has no attribute 'stop'
    def on_ticks(ws, ticks):
    # Callback to receive ticks.
    print("here3")
    print(ticks)
    print("#######################")
    print(ticks[0])
    print("#######################")
    print(ticks[0]['last_price'])
    print("#######################")

    global tick_count
    tick_count = tick_count +1
    print("TICK COUNT : ")
    print(tick_count)
    if(tick_count > 1):
    print("inside if")
    ws.unsubscribe([53718535])
    ws.close()

    def on_connect(ws, response):
    # Callback on successful connect.
    # Subscribe to a list of instrument_tokens (RELIANCE and ACC here).
    try:
    print("here0")
    ws.subscribe([53718535])
    print("here1")

    # Set Crude to tick in `full` mode.
    ws.set_mode(ws.MODE_FULL, [53718535])
    print("here2")
    except Exception:
    print(Exception)

    def on_close(ws, code, reason):
    print("here")
    ws.stop()


    # Assign the callbacks.
    kws.on_ticks = on_ticks
    kws.on_connect = on_connect
    kws.on_close = on_close

    kws.connect()
    print("################ HEREEEEEE #############################")
    tick_count = 0
    kws.connect()
  • zartimus
    @pracas KiteTicker has stop meth. Can you make sure that you're using the latest kiteconnect? https://pypi.org/project/kiteconnect/. Also, the 2nd `kws.connect()` will be an issue because you cant restart the websocket in the same python process.
Sign In or Register to comment.