1

Could anyone make me understand what is this error and what is going wrong and how to resolve it. I am quite new to python and learning and wanted to implement some multiprocessing in the code so started with the basic approach of it.

> 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

My code is below:

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)
davidism
  • 121,510
  • 29
  • 395
  • 339
U Bhargava
  • 23
  • 1
  • 2
  • 6

1 Answers1

6

Multiprocessing uses pickle to serialize and transfer data between sub-processes.

Pickle cannot serialize local (inner) functions. Try setting your target to a function visible from the file's namespace.

For example:

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)

Make sure s1, s2, and Symbols are defined.

Bharel
  • 23,672
  • 5
  • 40
  • 80