Works Great now. Also, I am trying to calculate candles using a script but if this disconnect and reconnect happens in the middle, I get wrong data in my calculations. Any solution you can think of for this problem?
I am facing freq disconnect and re connection is also not working. The logs as below
2019-08-13 11:13:22,003 Connection closed: 1006 - connection was closed uncleanly (WebSocket opening handshake timeout (peer did not finish the opening handshake in time))
Some times the reconnect works, but some times it just gets struck. How to solve this issue. Below is the kws initialization code.
I am facing freq disconnect and re connection is also not working. The logs as below
2019-08-13 11:13:22,003 Connection closed: 1006 - connection was closed uncleanly (WebSocket opening handshake timeout (peer did not finish the opening handshake in time))
Some times the reconnect works, but some times it just gets struck. How to solve this issue. Below is the kws initialization code.
@cheko Are you blocking on_tick method with any tick manipulation inside on_tick method? If you are then,you need to pass it to another thread, check multi threading example here.
@rakeshr Not sure if it is blocking of not , but i am writing the ticks to mysql DB as per below code. insert_ticks is from another .py file which writes to db.
@cheko, You are blocking the thread. The magnitude of the load depends on the number of instruments you are dealing with. If you have more writes to do then your program becomes unavailable for receiving next ticks.
@cheko Yeahinsert_ticks method call seems to be blocking on_tick method.Can you create separate thread for that and pass on the tick.You may check example here.
@cheko - please try to use dict or df and write them periodically .. this will give you breather for other executions. Real time writing along with threading will require cautious approach to flow coding .....
def on_connect(ws, response): # Callback on successful connect. # Subscribe to a list of instrument_tokens (RELIANCE and ACC here). ws.subscribe(tokens)
def on_close(ws, code, reason): # On connection close stop the main loop # Reconnection will not happen after executing `ws.stop()` #ws.stop() print("closing connection due to error code : %s and reason : %s" %(code,reason))
# Callback when reconnect is on progress def on_reconnect(ws, attempts_count): print("Reconnecting: {}".format(attempts_count)) logging.info("Reconnecting: {}".format(attempts_count))
# Callback when all reconnect failed (exhausted max retries) def on_noreconnect(ws): logging.info("Reconnect failed.")
# 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)
if kws.is_connected(): logging.info("### connected") else: print("Print this")
You can have look to this thread.If not resolved by above method,paste your WebSocket code here.
2019-08-13 11:13:22,003 Connection closed: 1006 - connection was closed uncleanly (WebSocket opening handshake timeout (peer did not finish the opening handshake in time))
Some times the reconnect works, but some times it just gets struck. How to solve this issue. Below is the kws initialization code.
# Initialise
kws = KiteTicker(api_key, access_token,reconnect=True,connect_timeout=60)
2019-08-13 11:13:22,003 Connection closed: 1006 - connection was closed uncleanly (WebSocket opening handshake timeout (peer did not finish the opening handshake in time))
Some times the reconnect works, but some times it just gets struck. How to solve this issue. Below is the kws initialization code.
# Initialise
kws = KiteTicker(api_key, access_token,reconnect=True,connect_timeout=60)
Are you blocking on_tick method with any tick manipulation inside on_tick method?
If you are then,you need to pass it to another thread, check multi threading example here.
Not sure if it is blocking of not , but i am writing the ticks to mysql DB as per below code. insert_ticks is from another .py file which writes to db.
def on_ticks(ws, ticks):
# Callback to receive ticks
insert_ticks(ticks)
logging.debug("Ticks: {}".format(ticks))
You are blocking the thread. The magnitude of the load depends on the number of instruments you are dealing with. If you have more writes to do then your program becomes unavailable for receiving next ticks.
Yeah
insert_ticks
method call seems to be blocking on_tick method.Can you create separate thread for that and pass on the tick.You may check example here.the sql is a Local hosted server.
@sujith
i am getting 58 instruments as of now. Just want to understand, will blocking the thread cause the Socket to disconnect?
@rakeshr Thanks, i will try the threaded method and update how it goes.
BTW today there were ~30 disconnections, but it managed to reconnect on its own.
Real time writing along with threading will require cautious approach to flow coding .....