我想在C中嵌入python并从C调用相同python脚本的多个实例。我该怎么做?
任何示例代码都会有所帮助.GIL如何影响我?
谢谢..
这是示例程序....哪个给出了错误的结果..因为我只有一个解释器......我需要调用多个解释器......我该怎么做?
-------------------C Program----------------
#include "Python.h"
int main(int argc, char* argv[])
{
PyObject *pName, *pModule[2], *pFunc;
PyObject *pArgs, *pValue;
int i = 0;
int a, b;
Py_SetProgramName(argv[0]);
Py_Initialize(); /* initialize the python interpreter */
for ( i = 0; i < 2; i++) {
pName = PyString_FromString("scr");
pModule[i] = PyImport_Import(pName);
if (pModule[i] != NULL) {
pFunc = PyObject_GetAttrString(pModule[i], "add");
if (pFunc && PyCallable_Check(pFunc)) {
printf ("Enter the numbers to add to run: ");
scanf ("%d %d", &a, &b);
pArgs = PyTuple_New(2);
pValue = PyInt_FromLong (a);
PyTuple_SetItem(pArgs, 0, pValue);
Py_DECREF(pValue);
pValue = PyInt_FromLong (b);
PyTuple_SetItem(pArgs, 1, pValue);
Py_DECREF(pValue);
pValue = PyObject_CallObject(pFunc, pArgs);
Py_DECREF(pArgs);
if (pValue != NULL) {
printf("Result of call: %ld\n", PyInt_AsLong(pValue));
Py_DECREF(pValue);
}
else {
Py_DECREF(pFunc);
Py_DECREF(pModule[i]);
PyErr_Print();
fprintf(stderr,"Call failed\n");
return 1;
}
}
}
else {
if (PyErr_Occurred())
PyErr_Print();
fprintf(stderr, "Cannot find function \n");
}
Py_DECREF(pFunc);
}
/* Print result */
for ( i = 0; i < 2; i++) {
// pName = PyString_FromString("scr");
//pModule[i] = PyImport_Import(pName);
if (pModule[i] != NULL) {
pFunc = PyObject_GetAttrString(pModule[i], "print_result");
if (pFunc && PyCallable_Check(pFunc)) {
pValue = PyObject_CallObject(pFunc, NULL);
if (pValue != NULL) {
printf("Call succeeded\n");
}
else {
printf("Call succeeded\n");
PyErr_Print();
fprintf(stderr,"Call failed\n");
return 1;
}
}
}
}
Py_Finalize();
return 0;
}
--------------- Python Script ---------------------
$ cat scr.py
c = 0
a = 0
b = 0
def add(a1,b1):
global a
global b
global c
a = a1
b = b1
print "Python: Will compute ", a,"+", b
c = a + b
return c
def print_result():
print 'Python: Result of ' , a , ' + ' , b, 'is: ', c
return
---------------- Actual Output-------------------
Enter the numbers to add to run: 2 3
Python: Will compute 2 + 3
Result of call: 5
Enter the numbers to add to run: 5 5
Python: Will compute 5 + 5
Result of call: 10
Python: Result of 5 + 5 is: 10 <--- PROBLEM
Call succeeded
Python: Result of 5 + 5 is: 10
Call succeeded
-------------------- Expected Output --------------------
Enter the numbers to add to run: 2 3
Python: Will compute 2 + 3
Result of call: 5
Enter the numbers to add to run: 5 5
Python: Will compute 5 + 5
Result of call: 10
Python: Result of 2 + 3 is: 5
Call succeeded
Python: Result of 5 + 5 is: 10
Call succeeded
-------------------------------------------------------
答案 0 :(得分:0)
通过导入Python.h
并调用Py_Initialize
,您将创建一个全局Python解释器。这将保留已加载模块的字典:第二次尝试导入同一模块时,该字典将返回与第一次相同的模块。这就是Python(和嵌入式Python)通常需要工作的方式 - 就像在Python脚本中进行第二次导入一样。
可能使用“sub interpreter”api通过调用Py_NewInterpreter()
创建一个新的子解释器。这可以用作具有自己的新模块字典等的新解释器。您可以使用PyThreadState_Swap()
在子解释器之间交换。
但是,我没有这样做,看起来有点记录不清。有一些文档,特别是一些警告here。显然,副译员并不是完全分开的,尽管它们可能足以满足您的目的。
在你的情况下,我认为你不必过分担心GIL,因为你不打算同时执行两个解释器。
所以我觉得这样的事情应该有用,虽然我没有测试过。
Py_Initialize();
// Do something in first interpreter
PyThreadState *tstate_1 = PyThreadState_Get();
PyThreadState *tstate_2 = Py_NewInterpreter();
// Do something in second interpreter
PyThreadState_Swap(tstate_1);
// Go back to using the first interpreter