Search code examples
pythonmultiprocessingpython-multiprocessing

Python multiprocessing giving two different pid for same object


i'm using Windows

import multiprocessing
import os
class BaseModule(multiprocessing.Process):
    def __init__(self):
        print("Initialize time pid: ",os.getpid())
        multiprocessing.Process.__init__(self)
        super().__init__()

    def get_pid(self):
        print("After new process pid: ",os.getpid())

    def run(self):
        self.get_pid()
        
if __name__ == '__main__':
    process = BaseModule()
    process.start()

OUTPUT:

Initialize time pid:  22148
After new process pid:  21244

In here same object get two different pid I need full object create and run in new process using multiprocessing(same pid)?


Solution

  • I Found a Way, in here I used a function for creating Object. Now I got same pid.

    import multiprocessing
    import os
    
    class BaseModule:
        def __init__(self):
            print("Initialize time pid: ",os.getpid())
    
        def get_pid(self):
            print("After new process pid: ",os.getpid())
    
        def run(self):
            self.get_pid()
    
    def use_multiprocessing():
        obj = BaseModule()
        obj.run()
    
    if __name__ == '__main__':
        process = multiprocessing.Process(target=use_multiprocessing)
        process.start()
    

    OUTPUT

    Initialize time pid:  18240
    After new process pid:  18240