1

I would like to run my python code from my C Code, and my python code launches 5 process in a same time Here my Python code compile.py:

from multiprocessing import Process 
import os, sys
import time

def info(title):
    print title
    print 'module name:', __name__
    if hasattr(os, 'getppid'):  # only available on Unix
        print 'parent process:', os.getppid()
    print 'process id:', os.getpid()

def f(name):
    info('function f')
    time.sleep(5)
    print 'hello', name

if __name__ == '__main__': #or def compileRun(processN)


    p1 = Process(target=f, args=('bob',))
    p2 = Process(target=f, args=('bob1',))
    p3 = Process(target=f, args=('bob2',))
    p4 = Process(target=f, args=('bob3',))
    p5 = Process(target=f, args=('bob4',))
    p1.start()
    p2.start()
    p3.start()
    p4.start()
    p5.start()
    p1.join()
    p2.join()
    p3.join()
    p4.join()
    p5.join()

My C Code

int Compile_Init(void **ppvPythonModule)
{
   void *pvPythonModule;

   _PyInit();

   pvPythonModule = PyImport_ImportModule((char *) "compile");


   *ppvPythonModule = pvPythonModule;
}
void Compile_Close(void *pvPythonModule)
{
   if (pvPythonModule)
   {
      PyDict_DelItemString(PyImport_GetModuleDict(), PyModule_GetName((PyObject *)pvPythonModule));
      Py_DECREF((PyObject *)pvPythonModul);
   }
}
Compile_LaunchProcess(void *pvPythonModule, char *pcString, int iProcessN)
{
   int iArgC = 1;
  char **ppcArgV = (char **)calloc(iArgC, sizeof(char *));

   Py_Main(iArgC, ppcArgV);
      //   PyObject *pFunc = PyObject_GetAttrString((PyObject *) pvPythonModule, "compileRun");

//       if (pFunc && PyCallable_Check(pFunc))
//       {
//          PyObject *pArgs = PyTuple_New(1);
// 
//          PyTuple_SetItem(pArgs, 0, PyInt_FromLong(iProcessN));
//          PyObject_CallObject(pFunc, pArgs);
//          Py_DECREF(pFunc);
//          if (pArgs)
//             Py_DECREF(pArgs);
//       }

   free(ppcArgV);

}

int main()
{
   void *pvPythonModule;


   Compile_Init(&pvPythonModule);

   Compile_LaunchProcess(pvPythonModule, 10);

   Compile_Close(pvPythonModule);

   return 0;

}

If I launch this code my exe give me a python input >> but does not launch my subprocess If I launch function compileRun instead of main my code runs my main process instead of python process

Could you please help me to have C code which called N python process on Windows which run my python f function Thanks

1 Answer 1

1

Finally, I have succeed with the following python code :

def compileRun(processN):
    processN = processN + 1
    plist= [0] * processN
    outList= [0] * processN
    errList= [0] * processN
    for i in range(1,processN):
       plist[i] = subprocess.Popen(["MyExe", "Myarg"], stdout=subprocess.PIPE)

    for i in range(1,processN):
       outList[i], errList[i] = plist[i].communicate()
       print outList[i]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.