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 ufuncs 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
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.)