How to let the child process of one function finish and then run the second function?

Viewed 12

Here I am simply calling a 3rd party API to get the prices of stocks through multiprocessing. I am using this function multiple times as I want the timeframe of stocks as (5 min, 10 min, 30 min). But when I run it, it does not wait for the previous functions to finish and instead move on to the last function to complete it. How to run each and every function in order ?

import pickle
import pandas as pd
import datetime
import multiprocessing
import time
import subprocess,os

def historical_data(timeframe):
    global prices
    def split_dict_equally(input_dict, chunks=2):
        "Splits dict by keys. Returns a list of dictionaries."
        # prep with empty dicts
        return_list = [dict() for idx in range(chunks)]
        idx = 0
        for k,v in input_dict.items():
            return_list[idx][k] = v
            if idx < chunks-1:  # indexes start at 0
                idx += 1
            else:
                idx = 0
        return return_list

    with open('zerodha_login.pkl', 'rb') as file:   
        # Call load method to deserialze
        login_credentials = pickle.load(file)
        
    with open('zerodha_instruments.pkl', 'rb') as file:   
        # Call load method to deserialze
        inst_dict = pickle.load(file)

    csv = pd.read_csv('D:\\Business\\Website\\Trendlines\\FO Stocks.csv')
    csv['Stocks'] = csv['Stocks'].str.replace(' ','') 
    fo_stocks = csv['Stocks'].to_list()
    inst = pd.DataFrame(inst_dict)
    filtered_inst = inst.copy()
    filtered_inst = inst[(inst['segment'] == 'NSE') & (inst['name'] != '') & (inst['tick_size'] == 0.05) ]
    filtered_inst = filtered_inst[filtered_inst['tradingsymbol'].isin(fo_stocks)]
    tickers_dict = dict(zip(filtered_inst['instrument_token'],filtered_inst['tradingsymbol']))
  
    tickers_dict = dict(zip(filtered_inst['instrument_token'],filtered_inst['tradingsymbol']))
    number_process = 16
    tickers_dict_list = split_dict_equally(tickers_dict,number_process)

    def prices(stock):
        print('inside_function',os.getpid())

        for x,y in stock.items():
            print('inside_stock_loop')
            while True:
                try:
                    print('Timeframe::',timeframe,y)
                    data = login_credentials['kite'].historical_data(instrument_token=x, from_date=today_date - datetime.timedelta(days=1000),interval=str(timeframe),to_date=today_date )
                    df = pd.DataFrame(data)
                    g = [e for e in df.columns if 'Un' not in e]
                    df = df[g]
                    df['date'] = df['date'].astype(str)
                    df['date'] = df['date'].str.split('+')
                    df['Date'] = df['date'].str[0]
                    df = df[['Date','open','high','low','close','volume']]
                    df['Date'] = pd.to_datetime(df['Date'],format='%Y-%m-%d %H:%M:%S')
                    df['Time'] = df['Date'].dt.time
                    df['Date'] = df['Date'].dt.date
                    df.rename(columns={'open':'Open','high':'High','low':'Low','close':'Close','volume':'Volume'},inplace=True) 
                    df.to_csv('D:\\Business\\Website\\Trendlines\\4th Cut\\Historical data\\'+str(timeframe)+'\\'+str(y)+'.csv')

                    break
                except:
                    print('Issue ::',y)
                    pass
    new_list = []
    if __name__ == '__main__':
        for process in tickers_dict_list: 
            p = multiprocessing.Process(target=prices, args=(process,))
            p.start()
            new_list.append(p)

    for p in new_list:
        print('joining_',p)
        p.join()

historical_data('5minute')
historical_data('10minute')
0 Answers
Related