简体   繁体   English

在 Python 中使用 Pool 类进行多处理会导致 Pickling 错误

[英]Multiprocessing using Pool class in Python giving Pickling error

I am trying to run a simple multiprocessing example in python3.6 in a zeppelin notebook(in windows) but I am not able to execute it.我正在尝试在 zeppelin 笔记本(在 Windows 中)中的 python3.6 中运行一个简单的多处理示例,但我无法执行它。 Below is the code that i used:下面是我使用的代码:


def sqrt(x):
    return x**0.5

numbers = [i for i in range(1000000)]
with Pool() as pool:
    sqrt_ls = pool.map(sqrt, numbers)

After running this code I am getting the following error:运行此代码后,我收到以下错误:

Traceback (most recent call last):
  File "/tmp/zeppelin_python-3196160128578820301.py", line 315, in <module>
    exec(code, _zcUserQueryNameSpace)
  File "<stdin>", line 6, in <module>
  File "/usr/lib64/python3.6/multiprocessing/pool.py", line 266, in map
    return self._map_async(func, iterable, mapstar, chunksize).get()
  File "/usr/lib64/python3.6/multiprocessing/pool.py", line 644, in get
    raise self._value
  File "/usr/lib64/python3.6/multiprocessing/pool.py", line 424, in _handle_tasks
    put(task)
  File "/usr/lib64/python3.6/multiprocessing/connection.py", line 206, in send
    self._send_bytes(_ForkingPickler.dumps(obj))
  File "/usr/lib64/python3.6/multiprocessing/reduction.py", line 51, in dumps
    cls(buf, protocol).dump(obj)
_pickle.PicklingError: Can't pickle <function sqrt at 0x7f6f84f1a620>: attribute lookup sqrt on __main__ failed

I am not sure if its just me who is facing the issue.我不确定是否只有我面临这个问题。 As i have seen so many articles where people can run the code easily.正如我看到的很多文章,人们可以轻松地运行代码。 If you know the solution please help如果您知道解决方案,请帮助

Thanks谢谢

From the multiprocessing documentation :多处理文档

Note: Functionality within this package requires that the main module be importable by the children.注意:此包中的功能要求模块可由子模块导入。 This is covered in Programming guidelines however it is worth pointing out here.这在编程指南中有介绍,但值得在这里指出。 This means that some examples, such as the Pool examples will not work in the interactive interpreter.这意味着某些示例(例如 Pool 示例)在交互式解释器中不起作用。

Notebooks are running Python interactive interpreters behind the scene, so it's probably why you get this error. Notebooks 在幕后运行 Python 交互式解释器,因此这可能是您收到此错误的原因。 You can try to run your code from within a if __name__ == '__main__': statement.您可以尝试从if __name__ == '__main__':语句中运行您的代码。

A Zeppelin notebook does not emulate a normal module well enough to support the pickling that is used to identify the correct operation to another process. Zeppelin 笔记本不能很好地模拟正常模块以支持用于识别另一个进程的正确操作的酸洗。 You can put all the functions you want to call into a proper module that you import in the usual fashion.您可以将要调用的所有函数放入以通常方式import的适当模块中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM