【问题标题】:Python Run main python multiprocess From CPython从C运行主python多进程
【发布时间】:2015-02-03 18:51:02
【问题描述】:

我想从我的 C 代码运行我的 python 代码,我的 python 代码同时启动 5 个进程 这是我的Python代码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()

我的 C 代码

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;

}

如果我启动此代码,我的 exe 会给我一个 python 输入 >> 但不会启动我的子进程 如果我启动函数 compileRun 而不是 main 我的代码运行我的主进程而不是 python 进程

能否请您帮我获得在 Windows 上调用 N python 进程的 C 代码,它运行我的 python f 函数 谢谢

【问题讨论】:

    标签: python c python-2.7 multiprocessing


    【解决方案1】:

    最后, 我已经成功使用了以下 python 代码:

    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]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-18
      • 1970-01-01
      • 1970-01-01
      • 2019-07-16
      • 2014-04-21
      相关资源
      最近更新 更多