C-API和python:运行python脚本的多个实例

时间:2015-10-07 03:20:29

标签: python c

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

这个程序有什么错误?任何有关解释的帮助将不胜感激..谢谢专家....

-------------------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 个答案:

没有答案