酸洗具有的PyObject *成员定义为C结构Python的扩展型 [英] Pickling a Python Extension type defined as a C struct having PyObject* members

查看:283
本文介绍了酸洗具有的PyObject *成员定义为C结构Python的扩展型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过Python C ++ code和想腌制的扩展类型。

所以,我必须包含指向一个数据库对象和对象管理对象(包括C ++编写的),我包裹着一个Python类型的对象(t_db_manager)C ++的结构(py_db_manager)。我的问题是,这种蟒蛇类型需要知道如何腌制两个指针,以将其发送给一些孩子多核进程。所以我注册的copy_reg模块类型(这相当于上式写的减少()方法。不过,我也不太清楚要放什么东西在里面。我应该建立一个元组用的PyObject *或仅仅是整数的指针?谁能帮助?

  typedef结构
{
  PyObject_HEAD
  *的PyObject man_inst_;
  *的PyObject db_inst_;} py_db_manager;`

这里的Py_TypeObject

  PyTypeObject t_db_manager = {
    PyObject_HEAD_INIT(0)/ * tp_head * /
    0,/ * tp_internal * /
    .py_db_manager,/ * tp_name * /
    的sizeof(py_db_manager)};

和这里的code,这将是在减少方式:

 的PyObject * pickle_manager(*的PyObject模块的PyObject *参数)
{
    py_db_manager * cpp_manager = 0;
    的PyObject *值= NULL,
        *元组= NULL;
    字符文本[512];    如果(PyArg_ParseTuple(参数,啊!,&安培;!t_db_manager,&安培; cpp_manager))
        转到错误;
    sprintf的(文字,man_inst_,db_inst_);
    如果((值= Py_BuildValue((SII),文本
                                cpp_manager-> man_inst_,cpp_manager-> db_inst_))== NULL)
        转到错误;
    元组= Py_BuildValue((OO),manager_constructor,价值观);错误:
    Py_XDECREF(值);
    返回元组;
}


解决方案

由于这将被传递到另一个过程,酸洗只是整数指针将无法正常工作,你会希望它。不同的进程使用不同的内存空间,因此他们没有的的同样的事情。

因此​​,要回答你的问题,你应该充分咸菜对象,并从接收端重建他们。

I am running C++ code via Python and would like to pickle an extension type.

So I have a C++ struct (py_db_manager) containing pointers to a database object and a object manager object (both written in C++) that I wrapped with a python type object (t_db_manager). My problem is that this python type needs to know how to pickle the two pointers in order to send it to some child multicore processes. So I registered the type with the copy_reg module (this is equivalent to writing a reduce() method on the type. However, I'm not too sure what to put in it. Should I build a tuple with the PyObject* or just the integer pointers?. Can anyone help?

typedef struct
{
  PyObject_HEAD
  PyObject* man_inst_ ;
  PyObject* db_inst_ ;

}py_db_manager;`

Here's the Py_TypeObject

PyTypeObject t_db_manager = {
    PyObject_HEAD_INIT(0)               /* tp_head */
    0,                                  /* tp_internal */
    ".py_db_manager",                  /* tp_name */
    sizeof(py_db_manager)};

And here's the code that would be in the reduce method:

PyObject *pickle_manager(PyObject *module, PyObject *args)
{
    py_db_manager *cpp_manager =0;
    PyObject *values = NULL,
        *tuple = NULL;
    char text[512];

    if (!PyArg_ParseTuple(args, "O!", &t_db_manager, &cpp_manager))
        goto error;
    sprintf(text,"man_inst_, db_inst_");
    if ((values = Py_BuildValue("(sii)", text,
                                cpp_manager->man_inst_, cpp_manager->db_inst_)) == NULL)
        goto error;
    tuple = Py_BuildValue("(OO)", manager_constructor, values);

error:
    Py_XDECREF(values);
    return tuple;
}

解决方案

Because this will be passed to another process, pickling just integer pointers will not work as you would want it to. Different processes use different memory space therefore they don't see the same things.

So, to answer your question, you should pickle full objects and reconstruct them from the receiving end.

这篇关于酸洗具有的PyObject *成员定义为C结构Python的扩展型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆