Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cython classes are non-pickable #34

Open
smcantab opened this issue May 28, 2014 · 0 comments
Open

Cython classes are non-pickable #34

smcantab opened this issue May 28, 2014 · 0 comments

Comments

@smcantab
Copy link
Contributor

Pele Cython classes are currently non pickable, they require a reduce method to be able to recreate a copy of the original object after being unpicked. This is because Pickle does not know anything about the nature of extension types. I came across this problem trying to parallelise some code with multiprocessing.

Official documentation about the reduce method is available here
https://docs.python.org/2/library/pickle.html#pickling-and-unpickling-extension-types
I found it not very informative though due to lack of examples and in general not many examples are available on the web, this is a point lamented even by the cython developers.

This is a minimal example of the approach I have taken so far:

cdef class PickableCythonClass:
    cdef potential
    cdef coords
    cdef __cinit__(pot, x):
        cdef _pele.BasePotential potential = pot
        cdef np.ndarray[dtype='d'] coords = x
        self.potential = potential
        self.coords = x
   cdef __reduce__(self):
          return (PickableCythonClass,(self.potential, self.coords))
          #note that the attributes have been passed as a tuple

if you think there's a better approach, please let me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant