帮忙看看如何pickle ufunc?
我正在尝试使用 MPI 将模块广播到其他 python 进程。当然,模块本身是不可 pickleable 的,但 __dict__ 可以。目前,我正在腌制 __dict__ 并在接收过程中创建一个新模块。这与一些简单的自定义模块完美配合。然而,当我尝试使用 NumPy 执行此操作时,有一件事情我无法轻松 pickle:ufunc。
我读过这个帖子,建议对 ufunc 的 __name__ 和 __module__ 进行腌制,但它们似乎依赖于 numpy 的完全构建和呈现在他们重建它之前。我需要避免在接收过程中一起使用 import
语句,所以我很好奇提到的 getattr(numpy,name)
语句是否适用于模块尚未包含 ufunc
。
另外,我在 NumPy 文档中的 ufunc 上没有看到 __module__ 属性: http://docs.scipy.org/doc/numpy/reference/ufuncs.html
请问有什么帮助或建议吗?
编辑:抱歉,忘记包含上面提到的线程。 http://mail.scipy.org/pipermail/numpy-discussion /2007-1月/025778.html
I'm attempting to broadcast a module to other python processes with MPI. Of course, a module itself isn't pickleable, but the __dict__
is. Currently, I'm pickling the __dict__
and making a new module in the receiving process. This worked perfectly with some simple, custom modules. However, when I try to do this with NumPy, there's one thing that I can't pickle easily: the ufunc
.
I've read this thread that suggests pickling the __name__
and __module__
of the ufunc
, but it seems they rely on having numpy fully built and present before they rebuild it. I need to avoid using the import
statement all-together in the receiving process, so I'm curious if the getattr(numpy,name)
statement mentioned would work with a module that doesn't have ufunc
s included yet.
Also, I don't see a __module__
attribute on the ufunc
in the NumPy documentation:
http://docs.scipy.org/doc/numpy/reference/ufuncs.html
Any help or suggestions, please?
EDIT: Sorry, forgot to include thread mentioned above. http://mail.scipy.org/pipermail/numpy-discussion/2007-January/025778.html
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Pickling Python 中的函数只会序列化它的名称和它来自的模块。它不会通过网络传输代码,因此在 unpickle 时,您需要拥有与 pickling 时相同的可用库。在 unpickle 时,Python 只需导入有问题的模块,并通过 getattr 获取项目。 (这不仅限于 Numpy,还适用于一般的 pickle。)
Ufunc 不能干净地 pickle,这是一个缺点。然后,您的选择主要是仅腌制 ufunc 的 __name__ (也可能是 __class__ ),然后手动重建它们。 (它们实际上不是 Python 函数,并且没有
__module__
属性。)Pickling a function in Python only serializes its name and the module it comes from. It does not transport code over the wire, so when unpickling you need to have the same libraries available as when pickling. On unpickling, Python simply imports the module in question, and grabs the items via
getattr
. (This is not limited to Numpy, but applies to pickling in general.)Ufuncs don't pickle cleanly, which is a wart. Your options mainly are then to pickle just the
__name__
(and maybe the__class__
) of the ufunc, and reconstruct them later on manually. (They are not actually Python functions, and do not have a__module__
attribute.)