使用Python.h从c++调用Django

Calling Django from C++ using Python.h

本文关键字:调用 Django c++ Python 使用      更新时间:2023-10-16

我需要从c++调用Django函数,并在我的c++代码中获得它们的输出。如你所知,Django是通过调用Manage.py Python脚本来使用的,其中所需的函数是第一个参数,后面是参数。

现在我有一个名为"test"的测试函数,它返回json格式的字符串。

因此如果我调用:

    python manage.py test

得到如下结果:

   {'products': [{'id': 125, 'label': 'Robin'}, {'id': 4, 'label': 'Robinou'}]}

要在cpp中得到这个,我可以:

   system(python path.../manage.py test);

并捕获输出。但是我不喜欢它。我也可以用boost。python。我也不喜欢。

出于大小和速度的考虑,我希望只能在c++/Python.h中做到这一点。

这是我正在运行的manage.py:

   import os
   import sys
   sys.path.append('C:ds')
   if __name__ == "__main__":
       os.environ.setdefault("DJANGO_SETTINGS_MODULE", "frontoffice.settings")
   from django.core.management import execute_from_command_line
   execute_from_command_line(sys.argv)

如果我添加 print sys.argv ,我得到以下输出: ['manage.py', 'test']

所以我设法用一种讨厌的方式来做:

   bool initPython()
   {
       Py_Initialize();
       int check = 0;
       check = PyRun_SimpleString("import sys");
       check = PyRun_SimpleString("import os");
       check = PyRun_SimpleString("sys.path.append('C:\ds')");
       check = PyRun_SimpleString("os.environ.setdefault("DJANGO_SETTINGS_MODULE", "frontoffice.settings")");
       check = PyRun_SimpleString("from django.core.management import execute_from_command_line");
       if(check == 0) return true;
       else return false;
   }
   std::string execPythonCommand(std::string cmd)
   {
       std::string command = std::string("robin = ['dontreallycarewhatitis',       '").append(cmd).append("']");
       PyRun_SimpleString(command.c_str());
       PyRun_SimpleString("execute_from_command_line(robin)");
   }

没什么奇特的。具有跨平台的优势,但捕获输出是一个烂摊子,所以它的工作,但它不是我想要的。我希望能够直接获得execute_from_comand_line命令的输出。

这是我到目前为止想到的。运行initPython()后:

    PyObject* executeCmd = PyImport_Import(PyString_FromString("django.core.management"));
    PyObject* executeFunc = PyObject_GetAttrString(executeCmd, "execute_from_command_line");
    std::cout << "Imported function." << " isNULL: " << (executeFunc == NULL) << " isExecutable: " << PyCallable_Check((executeFunc)) << std::endl;

所以我设法得到execute_from_command_line函数的Cpp对象-它是可调用的。这就是它变得棘手的地方。我怎么称呼它?

系统。根据Python文档,argv是一个列表。所以我试着重新创建了完全相同的东西并命名为:

    PyObject* pRobin = PyList_New(2);
    PyObject* pString = PyString_FromString("test");
    PyObject* pString0 = PyString_FromString("manage.py");
    PyList_Append(pRobin,pString0);
    PyList_Append(pRobin,pString);
    PyObject* pValue = PyObject_CallObject(executeFunc, pRobin);
    //PyObject* pValue = PyObject_CallFunctionObjArgs(executeFunc, pRobin, NULL); //Doesn't work either

现在我什么都试过了。PyObject_CallObject, CallFunction, CallFunctionObjArgs,使用元组而不是列表,PyErr_Print()没有给我任何显式的东西。我真的不是Python专家,正在努力解决这个问题。我怎么能得到pValue存储测试例程的输出JSON字符串?

供参考:

    >>> inspect.getargspec(django.core.management.execute_from_command_line)
    ArgSpec(args=['argv'], varargs=None, keywords=None, defaults=(None,))

及时我将需要运行管理.py xxx(其中xxx是一个命令)频繁和快速,所以我真的想使用C/API,而不是做一些讨厌的工作室捕捉。

仅供参考,我在VS2012, Windows 8, Python 2.7,和PostGRE在后面。任何和所有的想法将非常感激。谢谢!

我对从c++调用Python一无所知,但是在Python中动态调用管理命令的方法是使用django.core.management.call_command