1

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)

5
  • Have you profiled your code to understand what part of it is really taking too much time to get executed? Are you CPU-bounded or IO-bounded? Based on that you will decide whether to use threads, async, or multiprocessing. Commented Aug 20, 2021 at 8:35
  • @alec_djinn I think here CPU will not take many roles because I am fetching price {LTP} from the server and adding it to my database. (every second 10-20 transactions) Commented Aug 20, 2021 at 9:00
  • Then you should use asyncio Commented Aug 20, 2021 at 11:52
  • @alec_djinn getting error while trying asyncio Commented Aug 22, 2021 at 5:05
  • that is quite vague. Please post your code and the error message Commented Aug 22, 2021 at 19:53

1 Answer 1

1

most Python implementations do not have true multi-threading, because they use global lock (GIL). So only one thread runs at a time. For I/O heavy applications it should not make difference. But if you need CPU heavy operations done in parallel (and I see that you use Panda - so the answer must be yes) - you will be better off staying with multi-process app.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.