I have a question about the return object value from python.
use pybind11 to interface C++ function from python3
use Dask to do distributed computing
The following are the progeam snippet.
// ———— BEGIN pyCmd1.cpp ————–
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <vector>
namespace py = pybind11;
class dbPrep {
public:
dbPrep() : m_type(-9999) {};
void setType(const int t) { m_type = t;}
int getType() const { return m_type; }
//
private:
int m_type;
};
PYBIND11_MODULE(pyCmd1, m) {
m.doc() = "pybind11 for dask data passing"; // Optional module docstring
//
py::class_<dbPrep>(m, "dbPrep")
.def(py::init<>())
//.def(py::init<int>())
.def("set_type", &dbPrep::setType)
.def("get_type", &dbPrep::getType)
// see https://github.com/pybind/pybind11/blob/master/docs/upgrade.rst
// "New API for defining custom constructors and pickling functions"
//
.def(py::pickle(
[](const dbPrep &self) { // __getstate__
return py::make_tuple();
},
[](py::tuple t) { // __setstate__, note: no `self` argument
return new dbPrep();
// or: return std::make_unique<Foo>(...); // return by holder
// or: return Foo(...); // return by value (move constructor)
}
)
)
;
}
// —– END pyCmd1.cpp ——————
//——– BEGIN pyCmd1.py —————–
import dask
from dask.distributed import Client
import pyCmd1
def init_db():
db = pyCmd1.dbPrep()
db.set_type(100)
print("@ init_db, type = ", db.get_type())
return db
`if __name__ == "__main__":
# Create a Dask client with 2 workers
client = Client(n_workers=2)
db_init = client.submit(init_db)
r1 = client.gather(db_init)
print("> db_init type = ", r1.get_type())
#
client.close()
// — END pyCmd1.py ———
Output :
@ init_db, type = 100
db_init type = -9999
A ‘dbPrep’ object is created at python function init_db() and member data ‘type’ is set to 100.
then return the object.
Question:
Why the returned object db_init’s value ‘type’ is not 100 but the initial value -9999?
`