Need help for multi threading

renjitharajan84
Hello, anyone please suggest a code to resolve my long pending issue, I know this is simple, but really dont know how to do it.
I am using a simple code to update the tick data to excel file, but frequently getting 1006 error (connection error). I know the reason is due to blocking "on_ticks" and need to offload the data export (to excel) task from "on_ticks" event using multithreading. but dont know how to do this. my code is given below, i really need help please..

[Kite Connect client : pykiteconnect]
---------------------------------------------------------
from kiteconnect import KiteConnect
from kiteconnect import KiteTicker
import datetime
import xlwings as xw

kws = ""
kite = ""

api_k = "xxxxx" # api_key
api_s = "xxxx" # api_secret
access_tocken = "xxxxxx"

def get_login(api_k, api_s):
global kws, kite
kite = KiteConnect(api_key=api_k)
kite.set_access_token(access_tocken)
kws = KiteTicker(api_k, access_tocken)

#call login function
get_login(api_k, api_s)

subscribe = []
trd_portfolio = {}

wb = xw.Book('StoxGene_py_Ver 1.6_Double.xlsm')
sht = wb.sheets['dStream']

def read_excel():
for x in range(2, 146):
name = sht.range('A'+str(x)).value
if name == None:
break
token = int(sht.range('B'+str(x)).value)
subscribe.append(token)
trd_portfolio[token] = {"name": name, "row": x}

read_excel()

def on_ticks(ws, ticks):
feed_data(ticks)

def on_connect(ws, response):
ws.subscribe(subscribe)
ws.set_mode(ws.MODE_QUOTE, subscribe)

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

def feed_data(ticks):
for tick in ticks:
token = tick['instrument_token']
row = trd_portfolio[token]['row']
sht.range('C'+str(row)).value = tick['last_price']
sht.range('D'+str(row)).value = tick['ohlc']['open']
sht.range('E'+str(row)).value = tick['ohlc']['high']
sht.range('F'+str(row)).value = tick['ohlc']['low']
sht.range('G'+str(row)).value = tick['ohlc']['close']
sht.range('H'+str(row)).value = tick['volume']
sht.range('I'+str(row)).value = datetime.datetime.now()
print(datetime.datetime.now())

kws.on_ticks = on_ticks
kws.on_connect = on_connect
kws.on_close = on_close
kws.connect()


please dont give me the already existing threads of errors of multithreading.. i have gone through almost all threads and could not find proper resolution. I need some example code to implement multithreading on above code.

thanks in advance.
ACR
Sign In or Register to comment.