Python 基本多处理 - 无法从输入中提取本地对象和 运行

Python multiprocessing basic - Can't pickle local object and Ran out of input

任何人都可以让我了解这个错误是什么,出了什么问题以及如何解决它。 我对 python 很陌生,并且想在代码中实现一些多处理,所以从它的基本方法开始。

> AttributeError: Can't pickle local object
> 'computation.abc.<locals>.s1'
>     Traceback (most recent call last):
>       File "<string>", line 1, in <module>
>       File "C:\Python\lib\multiprocessing\spawn.py", line 116, in spawn_main
>         exitcode = _main(fd, parent_sentinel)
>       File "C:\Python\lib\multiprocessing\spawn.py", line 126, in _main
>         self = reduction.pickle.load(from_parent)
>     EOFError: Ran out of input

我的代码如下:

import multiprocessing


class computation:
    def abc(self):

        try:
            """Some
             logic
             here"""


        except Exception as E:
            print('Error : ', E)

        def func1(sfunc1):

            """some
            logic
            here"""

        def func2(sfunc2):

            """some
            logic
            here"""

        def check(stk):

            p1 = multiprocessing.Process(target=func1, args=s1)  # s1 and s2 is a List
            p2 = multiprocessing.Process(target=func2, args=s2)
            p1.start()
            p2.start()
            p1.join()
            p2.join()

        check(Symbols)

Multiprocessing 使用 pickle 在子进程之间序列化和传输数据。

Pickle 无法序列化本地(内部)函数。尝试将您的 target 设置为从文件的命名空间可见的函数。

例如:

import multiprocessing


def abc(self):

    try:
        """Some
            logic
            here"""


    except Exception as E:
        print('Error : ', E)

def func1(sfunc1):

    """some
    logic
    here"""

def func2(sfunc2):

    """some
    logic
    here"""

def check(stk):

    p1 = multiprocessing.Process(target=func1, args=s1)  # s1 and s2 is a List
    p2 = multiprocessing.Process(target=func2, args=s2)
    p1.start()
    p2.start()
    p1.join()
    p2.join()

if __name__ == "__main__":  # Make sure only 1 process runs this.
    check(Symbols)

确保 s1s2Symbols 已定义。