Search code examples
pythonpython-3.xprocessmultiprocessingargs

How to initialize a Python multiprocessing Process whose target takes no args?


rough code:

from multiprocessing import Process

def getPAprofilesPages():
    #do stuff here

def getPBprofilesPages():       
    #do stuff here

P1 = Process(target = getPAprofilesPages, args = [] )
P2 = Process(target = getPBprofilesPages, args = [] )

Notice that the functions take no arguments. I've tried to set args equal to None, (), (,) and [] as seen above, as well as completely omit it from the initialization. In any event, I get the same error when trying to run P1.start() or P2.start() in the interpreter:

>>> Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\******\AppData\Local\Programs\Python\Python37\lib\multiprocessing\spawn.py", line 105, in spawn_main
    exitcode = _main(fd)
  File "C:\Users\******\AppData\Local\Programs\Python\Python37\lib\multiprocessing\spawn.py", line 115, in _main
    self = reduction.pickle.load(from_parent)
AttributeError: Can't get attribute 'getPAprofilesPages' on <module '__main__' (built-in)>

Solution

  • The following code works fine in a script

    
    
    def main():
        ...
        all your other code goes here
        ... 
        from multiprocessing import Process
        P1 = Process(target = getPAprofilesPages )
        P2 = Process(target = getPBprofilesPages )
        P1.start()
        P2.start()
    
    def getPAprofilesPages():
        #do stuff here
        pass
    
    def getPBprofilesPages():
        #do stuff here
        pass
    
    if __name__ == '__main__':
        main()
    

    However, you say you are running it in the interpreter, and that is where your problem lies with the fact that you can't use multiprocessing package in interactive Python.

    I know this wasn't the answer you were looking for, but it explains your error. You can read more in that link about a workaround.