对使用C+的python对象使用多处理时,uu init_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu中缺少参数+;类作为基类 我有一个C++类节点,我用BooS/Python来暴露给Python(是的,我知道这个节点很小,但是我把它翻译出来了,因为一些非常大的类是从它派生出来的)。根据,如果我实现getstate\u manages\u dict,我应该能够做到这一点。下面是我用来复制错误的最少代码量:
头文件:对使用C+的python对象使用多处理时,uu init_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu中缺少参数+;类作为基类 我有一个C++类节点,我用BooS/Python来暴露给Python(是的,我知道这个节点很小,但是我把它翻译出来了,因为一些非常大的类是从它派生出来的)。根据,如果我实现getstate\u manages\u dict,我应该能够做到这一点。下面是我用来复制错误的最少代码量:,python,c++,inheritance,multiprocessing,boost-python,Python,C++,Inheritance,Multiprocessing,Boost Python,头文件: 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(No
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; }
};
错误消息(更改路径以避免重新显示个人信息):
文件“path/testNode.py”,第34行,在
a=q.get()
get中第113行的文件“path/lib/python3.7/multiprocessing/queues.py”
返回\u ForkingPickler.loads(res)
TypeError:\uuuu init\uuuu()缺少3个必需的位置参数:“l”、“m”和“n”
对于纯python代码,或者如果我只是在队列中放置一个Node对象,仅使用从Node继承的类,这不会导致任何问题。有人知道怎么修吗?此外,我也找不到任何特定于此类问题的文档,因此任何其他文档也会很有用
谢谢我想明白了:
似乎继承C++会破坏纯Python类的泡菜支持。我需要在
TestNode
中添加以下方法:
__getinitargs__(self), __getstate__(self), __setstate__(self, state)
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'
__getinitargs__(self), __getstate__(self), __setstate__(self, state)