如何从C调用同一python脚本的多个实例?
我想在C中嵌入python,并从C中调用同一python脚本的多个实例。我该怎么做 任何示例代码都会很有帮助。。吉尔在这里对我有什么影响 谢谢 这里是示例程序。。。。这是错误的结果。。因为我只有一个翻译。。。我需要调用多个解释器。。。我该怎么做如何从C调用同一python脚本的多个实例?,python,Python,我想在C中嵌入python,并从C中调用同一python脚本的多个实例。我该怎么做 任何示例代码都会很有帮助。。吉尔在这里对我有什么影响 谢谢 这里是示例程序。。。。这是错误的结果。。因为我只有一个翻译。。。我需要调用多个解释器。。。我该怎么做 -------------------C Program---------------- #include "Python.h" int main(int argc, char* argv[]) { PyObject *pName, *p
-------------------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
-------------------------------------------------------
通过导入Python.h并调用Py_Initialize,可以创建一个全局Python解释器。这将保留一个已加载模块的字典:第二次尝试导入同一模块时,该字典将返回与第一次相同的模块。这就是Python和嵌入式Python通常的工作方式——就像在Python脚本中进行第二次导入一样
通过调用Py_NewInterpreter,可以使用子解释器api创建新的子解释器。然后可以将其用作具有自己的新模块字典等的新解释器。您可以使用PyThreadState_swap在子解释器之间进行交换
但是,我没有这样做,而且它看起来有点缺乏文档记录。这里有一些文档,特别是一些注意事项。显然,副口译员并不是完全分开的,尽管他们可能足以满足您的需要
在你的情况下,我认为你不必太担心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
GIL是无关紧要的。就像调用其他脚本一样调用它们。我的意思是如何在单线程程序中实现这一点。。我的意思是我想维护我使用PyImport\u导入API调用导入的模块句柄列表或数组。。。我将在for循环中调用PyImport\u Import调用来实例化脚本10次,例如。。。我将使用不同的参数调用脚本中的函数。。。我每次都得到相同的句柄,它会覆盖脚本的每个实例。你需要用这类信息展开你的问题。理想情况下包括一个。我已经添加了一个MVCE,它遇到了预期的问题。想知道如何修复它。你想实现什么?几乎可以肯定有更好的办法。