[英]Embedding python into a C++
I am a bit confused for my implementation.我对我的实现有点困惑。 I have a python script to be embedded into C++.
我有一个要嵌入到 C++ 中的 python 脚本。
import urllib.request
import ssl
import suds.transport.http
from suds.client import Client
class UnverifiedHttpsTransport(suds.transport.http.HttpTransport):
def __init__(self, *args, **kwargs):
super(UnverifiedHttpsTransport, self).__init__(*args, **kwargs)
def u2handlers(self):
handlers = super(UnverifiedHttpsTransport, self).u2handlers()
context = ssl.create_default_context()
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
handlers.append(urllib.request.HTTPSHandler(context=context))
return handlers
url="https://xxxxxest.com/datamanagement.asmx?WSDL"
client = Client(url, transport=UnverifiedHttpsTransport())
def ReadDataTest():
result = client.service.ReadTestData()
return result
def ReadGridData():
result = client.service.ReadGridData()
return result
def main():
result=ReadGridData()
print(result)
if __name__ == "__main__":
main()
Then sample_main.cpp
has plugin lib
sample_plugin.cpp.然后
sample_main.cpp
有plugin lib
sample_plugin.cpp。
sample_plugin.cpp has python embedded code as follows. sample_plugin.cpp 有如下 Python 嵌入代码。
Py_Initialize();
pName = PyUnicode_DecodeFSDefault("web_interface");
pModule = PyImport_Import(pName);
Py_DECREF(pName);
if (pModule != NULL) {
pFunc_readtest = PyObject_GetAttrString(pModule, "ReadDataTest");
if (pFunc_readtest && PyCallable_Check(pFunc_readtest)) {
pValue = PyObject_CallObject(pFunc_readtest, NULL);
if(pValue != NULL) {
printf("Result of call: %s\n", pValue);
Py_DECREF(pValue);
}
}
Py_XDECREF(pFunc_readtest);
Py_DECREF(pModule);
}else{
PyErr_Print();
printf("Can't call python \n");
}
Py_FinalizeEx();
When I run sample_main.exe, I have errors as当我运行 sample_main.exe 时,出现以下错误
Traceback (most recent call last):
File "/opt/nvidia/deepstream/deepstream-5.0/sources/apps/sample_apps/deepstream-test3/web_interface.py", line 2, in <module>
import ssl
File "/usr/lib/python3.6/ssl.py", line 101, in <module>
import _ssl # if we can't import it, let the error propagate
ImportError: /usr/lib/python3.6/lib-dynload/_ssl.cpython-36m-aarch64-linux-gnu.so: undefined symbol: PyExc_OSError
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
import apport.fileutils
File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
from apport.packaging_impl import impl as packaging
File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 24, in <module>
import apt
File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
import apt_pkg
ImportError: /usr/lib/python3/dist-packages/apt_pkg.cpython-36m-aarch64-linux-gnu.so: undefined symbol: PyExc_ValueError
Original exception was:
Traceback (most recent call last):
File "/opt/nvidia/deepstream/deepstream-5.0/sources/apps/sample_apps/deepstream-test3/web_interface.py", line 2, in <module>
import ssl
File "/usr/lib/python3.6/ssl.py", line 101, in <module>
import _ssl # if we can't import it, let the error propagate
ImportError: /usr/lib/python3.6/lib-dynload/_ssl.cpython-36m-aarch64-linux-gnu.so: undefined symbol: PyExc_OSError
The error is removed when I include Py_Initialize();当我包含 Py_Initialize(); 时,错误被删除; inside
sample_main.cpp
.在
sample_main.cpp
。
Can somebody explain why it is necessary to include Py_Initialize();有人可以解释为什么需要包含 Py_Initialize(); inside
sample_main.cpp
?在
sample_main.cpp
里面?
Is that the right way to do that?这是正确的方法吗? What are the things I need to care inside
里面有什么我需要关心的
sample_main.cpp
for memory leak or anything other issues?内存泄漏或任何其他问题?
In an application embedding Python, the Py_Initialize() function must be called before using any other Python/C API functions
在嵌入 Python 的应用程序中,必须在使用任何其他 Python/C API 函数之前调用 Py_Initialize() 函数
First words of the documentation, https://docs.python.org/3/c-api/init.html文档的第一句话, https://docs.python.org/3/c-api/init.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.