How to find market close and gracefully close KiteTicker and do housekeeping

sameer
Hi,
I want to do some housekeeping after market is closed. Right now when market closes, kiteTicker is stucked and I dont get any ticks. Is there anyway to handle this so I can know that marke is closed so I can close my kiteTicker connection and do some housekeeping ?

Are u (planning to) call "on_close" on market close so that people can do housekeeping in on_close() instead of waiting indefinitely on "on_ticks"

OR is there any other recommeded practise to exit gracefully from thread receiving "on_ticks" ?
  • Vivek
    I would suggest you to have your own map of exchange timings and act based on that. Closing connection after market is not an ideal solution but there can be callback with market events but currently we don't have any idea to implement.
  • sameer
    sameer edited January 2018
    @vivek Yes Vivek I have that map but how to close thread running KiteTicker.

    Once I call kiteTicker.connect(), I dont have control on that thread ? How to tell that thread to stop.

    -------------------------------------------
    Typical thread loop looks like:

    stop_signal = False
    while not stop_signal:
    ------ do work
    and externally set stop_signal = True

    So I have code that periodically checks if stop_signal == True

    -------------------------------------------
    But in case of KiteTicker, how can I write a code to check periodically if stop_signal == True as I dont have control on code executed by thread running KiteTicker[Except ON_* functions]

    Is there any ON_* function supported by KiteTicker where I can set that STOP_signal = True and tell kiteticker to close connection ?
  • sameer
    Error 1006 - connection was closed uncleanly (WebSocket closing handshake timeout (peer did not finish the opening handshake in time))
    if I try to simple close kiteTicker.close()
  • sameer
    sameer edited February 2018
    Hi @sujith @vivek
    Can u please explain this ?

    I have following code

    class Kite(threading.Thread):
    def __init__(self):
    • self.kiteticker = KiteTicker(...)
    • self.on_connect = ...

    def run(self):
    • self.kiteticker.connect()

    def close_kite_connection():
    • self.kiteticker.close(); # Should this not close kite ticker ?


    if __name__ == '__main__':
    1. kite_thread = Kite()
    2. kite_thread.start() # Launches thread that collects ticks from kite server
    3. sleep(100) # sleep for 100 seconds

    4. LINE-19: kite_thread.close_kite_connection()
    5. LINE-20: kite_thread.join()
    6. LINE-23: print("Done interacting with Kite Server")

    Question:
    I am not able to exit from kite_thread though I am calling "close" on kite ticker. So LINE-20 join blocks forever.
    How can I reach LINE-21 in above code by telling kite_ticker to close itself
  • sameer
    class TickThread(threading.Thread)

    def __init__(self):
    super.__init__(self)

    self.kiteTicker = KiteTicker(…)

    def run(self):
    self.kiteTicker.connect()

    def stop_gracefully(self):
    self.kiteTicker.close()

    def on_tick(ws, ticks):
    print(“Inside TICKS”)
    print (ticks)

    def on_connect(ws):
    print(“Inside CONNECT”)

    def on_close(ws):
    print(“Inside CLOSE”)



    if __name__ == ‘__main__’:
    tickThread = TickThread()
    tickThread.start()
    sleep(100)
    tickThread.stop_gracefully()
    # I can still see tick at this point though I have closed kiteTicker gracefully ?



    Hi @vivek , @sujith ,
    I have above sample code that launches ticker in new thread. After sleeping for 100 seconds, main thread try to stop kiteTicker by calling "CLOSE()"...But still I see ON_TICKS functions getting called. How should I close my kiteTicker gracefully and exit from tick thread ?
  • Vivek
    Vivek edited February 2018
    @sameer I think it reconnects automatically after you close. Can you try manually stopping retry before you close. Something like this
    def stop_gracefully(self):
    self.kiteTicker.stop_retry()
    self.kiteTicker.close()
  • dheerajkabra
    Hi,

    I am facing the exact same problem. I tried the `stop_retry()` and then `close()` as suggested above, but the connection throws below error:

    Connection error: 1006 - connection was closed uncleanly (WebSocket closing handshake timeout (peer did not finish the opening handshake in time))

    My use-case is to disconnect and reconnect if no tick data received for whatever reason. Below is essential code:

    kws.stop_retry()
    kws.close()
    time.sleep(10)


    ISSUES:

    1 - The disconnect is not graceful. I get above mentioned `Connection error: 1006` error.
    2 - While reconnecting, I get a new error as below and it never connects thereafter.:


    File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 917, in _bootstrap_inner
    self.run()
    File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
    File "/usr/local/lib/python3.7/site-packages/twisted/internet/base.py", line 1260, in run
    self.startRunning(installSignalHandlers=installSignalHandlers)
    File "/usr/local/lib/python3.7/site-packages/twisted/internet/base.py", line 1240, in startRunning
    ReactorBase.startRunning(self)
    File "/usr/local/lib/python3.7/site-packages/twisted/internet/base.py", line 748, in startRunning
    raise error.ReactorNotRestartable()
    twisted.internet.error.ReactorNotRestartable


    Could anyone help me on above please.
Sign In or Register to comment.