0

I have use case where I have to do some operations on an excel file in python.
There will be 2 processes running in parallel.
The main.py file will open an excel sheet and starts the 3 processes.

  1. First process - does some calculation on the data from excel.
  2. Second process - Continuously writes data in the excel at fixed frequency like every 1 min.

Now my requirement is when I start the processes I need to share the excel object between the processes.

I have referred to few examples online and written the below code:

from multiprocessing import Process


def fun(name):
    print(f'hello {name}')

def main():
    excelObj =  OpenExcelApp()  # def which opens the excel application and returns the excel object 

    input_process = Process(target=self.input_data, args=[excelObj])  #input_data def fetches data from db and writes it into the excel file

    triggerCalc_process = Process(target= self.trigger_calc, args=[excelObj]) #triggers for calculation

    input_process.start()
    triggerCalc_process.start()

if __name__ == '__main__':
    main()

Can anyone suggest me what am I missing or doing wrong here. Many thanks in advance.

CrazyCoder
  • 2,194
  • 10
  • 44
  • 91
  • I think your logic is correct, however i would make sure that your excel object has functions to refresh/re-read from the file and to utilize those functions in your processes. What exactly is your problem? A minor thing: according to the [multiprocessing docs](https://docs.python.org/3/library/multiprocessing.html), you hand over arguments to Threads as tuples, e.g.: `p = Process(target=f, args=('bob',))`, for your example it should be: `input_process = Process(target=self.input_data, args=(excelObj,))` – mnikley Mar 17 '22 at 14:29
  • @mnikley , yes it has all the functions which are required to do the operation. My exact problem is , it gives an error saying "cant pickle excelOperation object" , where excelOperation is the class which handles the excel operations. – CrazyCoder Mar 17 '22 at 14:35
  • Maybe your issue is related to [this](https://stackoverflow.com/a/62186493/12892026) – mnikley Mar 17 '22 at 15:54

0 Answers0