如何初始化目标不带参数的 Python 多处理进程?

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

粗略代码:

from multiprocessing import Process

def getPAprofilesPages():
    #do stuff here

def getPBprofilesPages():       
    #do stuff here

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

注意函数没有参数。 如上所示,我尝试将 args 设置为 None、()、(,) 和 [],并在初始化中完全省略它。 无论如何,我在解释器中尝试 运行 P1.start()P2.start() 时遇到相同的错误:

>>> 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)>

以下代码在脚本中运行良好



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()

但是,你说你在解释器中是运行,这就是你的问题所在,因为你不能使用multiprocessing package in interactive Python

我知道这不是您要找的答案,但它解释了您的错误。您可以在 link 中阅读有关解决方法的更多信息。