Rainmatter team: Best way to terminate websocket script in python for V3 api.

MAG
Hi Rainmatter team,

I have migrated my websocket script to kiteconnect3.0. In the earlier v2 script I used to call threaded= true.
Then in the main thread I used to check the time using a while loop and at 15:30, I used to call sys.exit().

In V3 I see that you have added blanket exception handling due to which everything including a sys.exit or even a CTRL + c is being captured and the script just goes on.
After 3:30 it ends up retrying connections when the ticks don't flow in a kind of a never ending loop. Yesterday I had to hit a CTRL+z and then do a "kill -9 %1"

I guess ws.stop() cannot be called from the main thread.
So my query to you is this. What is your recommended way to close the websocket and terminate the script at 15:30 from the main thread.
In the absence of a sandbox env, I don't want to experiment on this and end up hammering your prod servers unnecessarily.

  • MAG
    If i try either kws.close() or kws.stop or any combination thereof I get exceptions as follows:

    1520571840 Fri, 09 Mar 2018 10:34:00:041181 INFO on_ticks(): Tick written to redis key batch1 and mongo collection (count:2135)
    last tick: 2018-03-09 10:34:00.028404 timenow: 2018-03-09 10:34:00.044642 delta: 0:00:00.016238
    1520571840 Fri, 09 Mar 2018 10:34:00:044703 INFO Loop2: Time is 10:34 - Breaking loop 2 as market closed.
    ERROR:kiteconnect.ticker:Connection error: 1006 - connection was closed uncleanly (peer dropped the TCP connection without previous WebSocket closing handshake)
    ERROR:kiteconnect.ticker:Connection closed: 1006 - connection was closed uncleanly (peer dropped the TCP connection without previous WebSocket closing handshake)
    1520571840 Fri, 09 Mar 2018 10:34:00:045517 ERROR on_close(): ERROR : 1006, connection was closed uncleanly (peer dropped the TCP connection without previous WebSocket closing handshake)

  • Vivek
    Seems like issue with your implementation. I have tried the same and got the connection closed cleanly. Here is the logs with debug enabled
    2018-03-09 11:40:46,959 - kiteconnect.ticker - DEBUG - Start WebSocket connection.
    2018-03-09 11:40:46+0530 [-] Log opened.
    2018-03-09 11:40:47,318 - root - DEBUG - on connect: {"peer": "tcp4:13.127.123.140:443", "headers": {"date": "Fri, 09 Mar 2018 06:10:47 GMT", "connection": "upgrade", "upgrade": "websocket", "sec-websocket-accept": "DdxUBVc/waAR6gPASl9NrloiP1s="}, "version": 18, "protocol": null, "extensions": []}
    2018-03-09 11:40:47,319 - kiteconnect.ticker - DEBUG - ping =>
    2018-03-09 11:40:47,345 - kiteconnect.ticker - DEBUG - pong =>
    2018-03-09 11:40:47+0530 [-] got ticks: 19
    2018-03-09 11:40:47,434 - kiteconnect.ticker - ERROR - Connection closed: None - None
    2018-03-09 11:40:47+0530 [KiteTickerClientProtocol (TLSMemoryBIOProtocol),client] Abandoning on explicit request
    2018-03-09 11:40:47+0530 [-] Stopping factory
    2018-03-09 11:40:47+0530 [-] Main loop terminated.
    Here is callbacks implementation for `on_ticks` and `on_close`
    def on_ticks(ws, ticks):
    print("got ticks: {}".format(len(ticks)))
    ws.close()

    def on_close(ws, code, reason):
    ws.stop()
Sign In or Register to comment.