我想从C ++ DLL中调用一些python函数,而C ++ DLL是从C#应用程序中调用的。我能够跨语言发送双精度数组,字符串(在char数组中)而没有问题(通过列表将数组发送到python)。现在,我想在我称之为numpy和pandas的python脚本中使用一些数据分析。
此处,CPYInstance和CPyObject是帮助程序类,它们基本上称为Py_Initialize和Py_Finalize。
这些包装的来源: https://www.codeproject.com/Articles/820116/Embedding-Python-program-in-a-C-Cplusplus-code
double Train()
{
CPyInstance pyInstance;
CPyObject pModule = PyImport_ImportModule("build");
if (pModule)
{
CPyObject pFunc = PyObject_GetAttrString(pModule, "train");
if (pFunc && PyCallable_Check(pFunc))
{
CPyObject pTrainingData = PyList_New(0);
CPyObject pDate = PyList_New(0);
for (int i = 0; i < trainingDataSize; i++)
{
PyList_Append(pTrainingData, PyFloat_FromDouble(trainingData[i]));
PyList_Append(pDate, PyUnicode_FromStringAndSize(dateArray[i], DATE_SIZE));
}
if (pTrainingData && pDate)
{
CPyObject pValue = PyObject_CallFunctionObjArgs(pFunc, pTrainingData, pDate, NULL);
pFunc.Release();
pDate.Release();
pTrainingData.Release();
if (pValue)
{
double num = PyFloat_AsDouble(pValue);
return num;
}
else
{
return 1.01;
}
}
else
{
return 2.01;
}
}
else
{
return 3.01;
}
else
{
return 4.01;
}
}
python文件build.py看起来像这样:
import numpy as np
import pandas as pd
def train(X_train, date):
if(type(X_train) == list):
df = pd.DataFrame(data = X_train, columns = ['Feature'], index = date);
df.index.names = ['Date'];
df.to_csv('dataframe');
return 100
else:
return 110
当我从C#应用程序调用此函数时,错误是:
Exception ignored in: <module 'threading' from 'C:\\Users\\magic\\Anaconda3\\Lib\\threading.py'>
Traceback (most recent call last):
File "C:\Users\magic\Anaconda3\Lib\threading.py", line 1289, in _shutdown
assert tlock.locked()
SystemError: <built-in method locked of _thread.lock object at 0x00000216CB115238> returned a result with an error set
1.01