CAPI和python:运行python scrip的多个实例

2024-04-16 21:06:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python里的新蜜蜂。。 我需要从C程序中调用同一个Python脚本的多个实例。 我需要以异步方式调用它们(意思是随机调用)。为此,我编写了一个测试程序:在这里,我从C程序中读取2个数字,并调用python脚本来添加这些数字,并将其保存在全局变量中,以便以后可以再次调用该脚本来打印它们。我将在for循环中调用脚本两次..这样我可以添加两组数字。当我打印结果时,我看到我在脚本的第一个实例中存储的数字丢失了。请参阅下面的代码,预期输出和实际输出。你知道吗

这个节目有什么错误?任何帮助解释都将不胜感激。。感谢各位专家。。。。你知道吗

-------------------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
 -------------------------------------------------------

Tags: ofthetopyaddifresultcall