D
D
Dmitry Prilepsky2020-11-18 12:59:20
Python
Dmitry Prilepsky, 2020-11-18 12:59:20

Is it possible to run a class method in multiprocessing from the same class?

At once I will make a reservation that with multiprocessing I work the second time. I had such a problem. There is code that needs to be run in different processes. I understand that I can go up a level and run the initialization of a given class (create a new class object) and run it there. But I'm wondering if it's possible to run the start method in multiprocessing in the same class. I understand that each process will need its own place in memory. THOSE. each process accesses its memory cells and if we do not have a constructor assembly in each process, then nothing will work (correct me if I'm wrong). Is it possible to make it so that it works? one of the options that I had was to call the constructor again when creating a new process like:

process = list()
for thread in range(threads):
    __init__(self, program)
    process_object = Process(target=program.start, args=tuple())
    process_object.start()
    process.append(process_object)
for process in process:
    process.join()
for process in process:
    process.terminate()
)
But something reminds me that this is a bad tone and I'm not even sure that this can be done. Are there any other ways? I will be grateful for any advice. I am attaching the code.
class Prog(Concrete_Prog):
    def __init__(self, queue, range_='', threads=1):

        self.threads = threads
        self.range = range_
        self.factory = QueueFactory(DBS.DB_QUEUES, QUEUES.WORKER_QUEUE)
        self.getter = Browser()
        self.saver = MongoDBSaver(db_name=DBS.LIGHT,
                                  collection_name=COLLECTIONS.SAVE)

        self.program = Behavior(getter=self.getter,
                                       range=self.range,
                                       saver=self.saver,
                                       factory_queue=self.factory,
                                       threads=self.threads)
      

    def start(self):
        queue = self.factory.generate_queue()
        if queue.is_empty():
            self.generate_queue(queue, self.queue_generator)
        self.program.start

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question