使用多处理在类对象列表中应用方法

时间:2017-12-13 14:46:17

标签: python python-3.x multiprocessing

我创建了一个名为Sudoku的类,将数独存储在类实例中。我在这里主要是:

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)

我收到此错误:

  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

我做错了什么?

0 个答案:

没有答案