Need to auto restart KiteTicker in case disconnected

baruns
Hi,

I'm trying to write a code in Python3 which will fetch Market Data using KiteTicker and restart automatically in case the connection gets closed. So I've written below mentioned code. But it doesn't seem to be working at the time of restart as it is giving error "twisted.internet.error.ReactorNotRestartable". Please suggest on same as if the connection gets closed suddenly, I need to restart manually and I will loose market data in the time interval while it will not be running.

from kiteconnect import KiteTicker
import time

__api_key__ = ""
__access_token__ = ""
__ilist__ = [738561]

class DataLoader:

def __init__(self, api_key, access_token, ilist):
self.api_key = api_key
self.access_token = access_token
self.ilist = ilist

def main(self):
try:
self.t = KiteTicker(self.api_key, self.access_token, reconnect=True, reconnect_max_delay=5, reconnect_max_tries=300)
self.load_data()
except Exception as e:
print(e)

def save(self, ticks):
for tick in ticks:
print("Will be inserting to DB")

def on_ticks(self, ws, ticks):
self.save(ticks)

def on_connect(self, ws, response):
ws.subscribe(self.ilist)
ws.set_mode(ws.MODE_FULL, self.ilist)

def on_close(self, ws, code, reason):
ws.stop()

def on_error(self, ws, code, reason):
print("Error Code: " + str(code))
print("Error: " + str(reason))

def load_data(self):
self.t.on_ticks = self.on_ticks
self.t.on_close = self.on_close
self.t.on_error = self.on_error
self.t.on_connect = self.on_connect
self.t.connect(threaded=True)
while True:
if not self.t.is_connected():
self.t.connect(threaded=True)
time.sleep(3)



my = DataLoader(__api_key__, __access_token__, __ilist__)
my.main()
This discussion has been closed.