So, currently, I am using multiprocessing to run these 3 functions together.
As only tokens changes, is it recommended to switch to multi-threading? (if yes, will it really help in a performance like speed-up and I think memory will be for sure used less)
This is my code:
from database_function import * from kiteconnect import KiteTicker import pandas as pd from datetime import datetime, timedelta import schedule import time from multiprocessing import Process def tick_A(): #credentials code here tokens = [x[0] for x in db_fetchquery("SELECT zerodha FROM script ORDER BY id ASC LIMIT 50")] #FETCHING FIRST 50 SCRIPTS TOKEN #print(tokens) ##### TO MAKE SURE THE TASK STARTS AFTER 8:59 ONLY ########### t = datetime.today() future = datetime(t.year,t.month,t.day,8,59) if ((future-t).total_seconds()) < 0: future = datetime(t.year,t.month,t.day,t.hour,t.minute,(t.second+2)) time.sleep((future-t).total_seconds()) ##### TO MAKE SURE THE TASK STARTS AFTER 8:59 ONLY ########### def on_ticks(ws, ticks): global ltp ltp = ticks[0]["last_price"] for tick in ticks: print(f"{tick['instrument_token']}A") db_runquery(f'UPDATE SCRIPT SET ltp = {tick["last_price"]} WHERE zerodha = {tick["instrument_token"]}') #UPDATING LTP IN DATABASE #print(f"{tick['last_price']}") def on_connect(ws, response): #print(f"response from connect :: {response}") # Subscribe to a list of instrument_tokens (TOKENS FETCHED ABOVE WILL BE SUBSCRIBED HERE). # logging.debug("on connect: {}".format(response)) ws.subscribe(tokens) ws.set_mode(ws.MODE_LTP,tokens) # SETTING TOKEN TO TICK MODE (LTP / FULL / QUOTE) kws.on_ticks = on_ticks kws.on_connect = on_connect kws.connect(threaded=True) #####TO STOP THE TASK AFTER 15:32 ####### end_time = datetime(t.year,t.month,t.day,15,32) while True: schedule.run_pending() #time.sleep(1) if datetime.now() > end_time: break #####TO STOP THE TASK AFTER 15:32 ####### def tick_B(): everything remains the same only tokens value changes tokens = [x[0] for x in db_fetchquery("SELECT zerodha FROM script ORDER BY id ASC OFFSET (50) ROWS FETCH NEXT (50) ROWS ONLY")] def tick_C(): everything remains the same only tokens value changes tokens = [x[0] for x in db_fetchquery("SELECT zerodha FROM script ORDER BY id ASC OFFSET (100) ROWS FETCH NEXT (50) ROWS ONLY")] if __name__ == '__main__': def runInParallel(*fns): proc = [] for fn in fns: p = Process(target=fn) p.start() proc.append(p) for p in proc: p.join() runInParallel(tick_A , tick_B , tick_C) So, currently, I am using multiprocessing to run these 3 functions together.
As only tokens changes, is it recommended to switch to multi-threading? (if yes, will it really help in a performance like speed-up and I think memory will be for sure used less)