-1

我正在尝试使用pathos包运行一些多处理例程。这是我的代码:

import multiprocessing as mp
import time

def square(x):
    return x*x

if __name__=="__main__":
    start_time = time.time()
    p = mp.Pool(3)
    function_results = p.map(square,[1,2,3]) #ordered
    p.close()
    print("--- %s seconds ---" % (time.time() - start_time))

我正在尝试在 Win 10 上运行它并使用 Jupyter 笔记本。

代码未运行并生成以下错误:

Process SpawnPoolWorker-2:
Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\conda\conda\envs\tensorflow\lib\multiprocessing\process.py", line 252, in _bootstrap
    self.run()
  File "C:\Users\user\AppData\Local\conda\conda\envs\tensorflow\lib\multiprocessing\process.py", line 93, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\user\AppData\Local\conda\conda\envs\tensorflow\lib\multiprocessing\pool.py", line 108, in worker
    task = get()
  File "C:\Users\user\AppData\Local\conda\conda\envs\tensorflow\lib\multiprocessing\queues.py", line 337, in get
    return ForkingPickler.loads(res)
AttributeError: Can't get attribute 'square' on <module '__main__' (built-in)>

此代码在 Ubuntu 16.04 上运行良好。关于如何修复它的任何建议?

4

1 回答 1

3

尝试将函数square放在单独的文件中,然后将其导入,如本文所述

于 2018-01-03T16:46:47.690 回答