我在C++中有一个类Node,我使用Boost/python向python公开了它(是的,我知道Node很小,但我翻译了它,因为一些相当大的类是从它派生的(。根据boost/python文档,如果我实现getstate_manages_dict
,我应该能够做到这一点。以下是我用来复制错误的最少量代码:
头文件:
class Node{
public:
Node(boost::python::object position);
~Node();
boost::python::object _position;
};
// pickle support for Node
struct node_pickle_suite : boost::python::pickle_suite{
static boost::python::tuple getinitargs(Node const& node){
return boost::python::make_tuple(node._position);
}
static boost::python::tuple getstate(boost::python::object obj)
{
Node& node = boost::python::extract<Node&>(obj);
return boost::python::make_tuple(obj.attr("__dict__"));
}
static void setstate(boost::python::object obj, boost::python::tuple state)
{
Node& node = boost::python::extract<Node&>(obj);
boost::python::dict d = extract<dict>(obj.attr("__dict__"));
d.update(state[0]);
}
static bool getstate_manages_dict() { return true; }
};
cpp文件:
#include "node.h"
using namespace std;
Node::Node(boost::python::object position){
this->_position = position;
}
Node::~Node(){
}
BOOST_PYTHON_MODULE(Node){
class_<Node>("Node", init<boost::python::object>())
.def_readwrite("_position", &Node::_position)
.def_pickle(node_pickle_suite());
}
在python中,我有一个使用Node作为基类的类,以及一个需要将一些TestNode放入队列的进程。当q.put
工作时,q.get
不工作:
class Position:
def __init__(self, i, j ,k):
self._i = i
self._j = j
self._k = k
class TestNode(Node):
def __init__(self, position, l, m, n):
super().__init__(position)
self.l = l
self.m = m
self.n = n
self.c = {}
self.d = {}
self.dq = deque()
from multiprocessing import Process, Queue
def do_stuff(q):
for i in range(100):
pos = Position(1,2,3)
tn = TestNode(pos, 10, "hello", "world")
q.put(tn)
processes = []
q = Queue()
for i in range(3):
processes.append(Process(target=do_stuff, args=(q,)))
for process in processes:
process.start()
# the program crashes when this is called
a = q.get()
for process in processes:
process.join()
错误消息(更改路径以避免泄露个人信息(:
File "path/testNode.py", line 34, in <module>
a = q.get()
File "path/lib/python3.7/multiprocessing/queues.py", line 113, in get
return _ForkingPickler.loads(res)
TypeError: __init__() missing 3 required positional arguments: 'l', 'm', and 'n'
对于纯python代码,或者如果我只是将Node对象放在队列中,只使用从Node继承的类,这都不会造成任何问题。有人知道怎么修吗?此外,我找不到任何具体的问题,所以任何额外的文档也会很有用。
感谢
想好了:
从C++继承似乎会打破纯Python类附带的pickle支持。我需要在TestNode
中添加以下方法:
__getinitargs__(self), __getstate__(self), __setstate__(self, state)