0

I am stuck in using the multiprocessing module of Python. I wanted to create multiprocessing workers in a class and use the object of this class as a composite in another class. Below is the dummy code where the structure looks the same as my original code.

a.py

import concurrent.futures 
class A:
   def __init__(self, basic_input):
       self.initial = basic_input

   def _worker(self, work):
      print(f"Doing some {work} using {self.initial}")

   def print_my_work(self, set_of_works):
      with concurrent.futures.ProcessPoolExecutor(max_workers=5) as executor:
          executor.map(self._worker, set_of_works)

b.py

class B:
   def __init__(self, basic_input):
      # A is a composite object 
      self.a = A(basic_input)
  
   def get_all_works(self):
      return {'A', 'B', 'C'}
   
   def processingB(self):
      works = self.get_all_works()
      self.a.print_my_work(works)

Here I'm trying to use class B in another module as below

check.py

import b
obj = B('Test')
obj.processingB()

Getting below error Python multiprocessing RuntimeError: An attempt has been made to start a new process before the current process has finished its bootstrapping phase.......

Can somebody help me. Thank you for reading this question.

Nicos44k
  • 88
  • 7

1 Answers1

0

Googling your problem, youn find a similar post : It seems that you are using Windows and you should then add if __name__ == '__main__': at the start of you main script (to avoid creating subprocesses recursively) :

import B
if __name__ == '__main__':    
    obj = B('Test')
    obj.processingB()

Using your code I finally get :

Doing some A using Test
Doing some B using Test
Doing some C using Test
Nicos44k
  • 88
  • 7
  • Thanks a ton for your response. Do we have any other approach for this because we have hardcode 'Test' input in below code. I want to create object of B dynamically from another module for runtime inputs. Here we have hardcoded B.```if __name__ == '__main__': obj = B('Test') obj.processingB(). – Abhay Verma Aug 18 '21 at 08:30
  • The `if __name__ == '__main__':` just have to be placed on the first script you will call. You should look about python entry point : https://chriswarrick.com/blog/2014/09/15/python-apps-the-right-way-entry_points-and-scripts/ – Nicos44k Aug 18 '21 at 09:00