Applying a method in a list of class objects using

2019-07-29 00:08发布

问题:

I made a class called Sudoku witch stores the sudoku board within the class instance. Here i my main:

from multiprocessing import Pool
class Sudoku:
    ...

    def resi(self):
        ... solves the board


if __name__ == "__main__":
    def solve(t):
        t.resi()
        return t

    l = ... -  a list of unsolved sudoku boards
    p = Pool()
    l = p.map(solve, l)

I get this error:

  File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.5/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/usr/lib/python3.5/cProfile.py", line 160, in <module>
    main()
  File "/usr/lib/python3.5/cProfile.py", line 153, in main
    runctx(code, globs, None, options.outfile, options.sort)
  File "/usr/lib/python3.5/cProfile.py", line 20, in runctx
    filename, sort)
  File "/usr/lib/python3.5/profile.py", line 64, in runctx
    prof.runctx(statement, globals, locals)
  File "/usr/lib/python3.5/cProfile.py", line 100, in runctx
    exec(cmd, globals, locals)
  File "sudoku.py", line 94, in <module>
    lista = p.map(solve, lista)
  File "/usr/lib/python3.5/multiprocessing/pool.py", line 260, in map
    return self._map_async(func, iterable, mapstar, chunksize).get()
  File "/usr/lib/python3.5/multiprocessing/pool.py", line 608, in get
    raise self._value
  File "/usr/lib/python3.5/multiprocessing/pool.py", line 385, in _handle_tasks
    put(task)
  File "/usr/lib/python3.5/multiprocessing/connection.py", line 206, in send
    self._send_bytes(ForkingPickler.dumps(obj))
  File "/usr/lib/python3.5/multiprocessing/reduction.py", line 50, in dumps
    cls(buf, protocol).dump(obj)
_pickle.PicklingError: Can't pickle <function solve at 0x7f55e09d7158>: attribute lookup solve on __main__ failed

What am i doing wrong?