-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Making a C++ function pickleable? #1261
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
Comments
Need to use functors (in both C++ and Python wrappers) so that it could be serialized and reused with dask. pybind/pybind11#1261
Are there any methods to automatically set all the functions and classes pickeable? |
I'm working on a distributed database which has mobile Python code. The inability to pickle pybind11 functions forces all native function imports into the function that is being transferred, which is a pain and kinda messy. I'm gonna look at work arounds, but a pybind11 level fix would be great. |
I would also like to try to use py::pickle to support serialization for an object with a python callback of type std::function in c++ code. But it seems when it calling getstate, the error still throw up with "PyCapsule cannot be pickled." Here is how I reproduce this issue
|
Is there any news regarding making the pybind11 functions pickleable? |
FWIW, I found one way to work around this issue, with pybind11 as is: (I'm still exploring other options.) |
Uh oh!
There was an error while loading. Please reload this page.
Issue description
Is there a practical way to make a C++ function pickleable? I want to do this in order to support pickle on my own classes with
__reduce__
.In more detail, I am wrapping a hierarchy of C++ objects. I have a C++ factory function that will return a shared_ptr to a suitable object in the hierarchy given its serialization (the internal details don't matter to this question):
As such it is trivial to use
__reduce__
to add pickling support, and I think__getstate__
and__setstate__
would be a lot messier. I would like to add thisObject.__reduce__
:This compiles, and
Object.__reduce__()
runs correctly, but when I try to pickle objects I get this error:TypeError: can't pickle PyCapsule objects
I have worked around the problem by creating a functor class that does the same thing as
makeObject
and adding a__reduce__
method to that functor so it can be pickled:However, if somebody knows a simple way to add support to my
makeObject
function I could avoid the functor and its messy wrapper.I have included a simple example below. There is no class hierarchy and so no need for
__reduce__
, but it shows the issue.Reproducible example code
C++ Code
Python code
Results
This prints:
The text was updated successfully, but these errors were encountered: