`from multiprocessing import Pipe
from multiprocessing import Process
from multiprocessing import freeze_support
def worker():
while True:
print("subprocess !!!")
pass
def muti_work_init():
w = Process(target = worker)
w.start()
print("finish muti_work_init")
if name == 'main':
muti_work_init()`
This is consle output:
enter cpp main finish muti_work_init enter cpp main finish muti_work_init enter cpp main finish muti_work_init enter cpp main finish muti_work_init enter cpp main finish muti_work_init enter cpp main finish muti_work_init enter cpp main finish muti_work_init enter cpp main finish muti_work_init enter cpp main finish muti_work_init enter cpp main finish muti_work_init enter cpp main finish muti_work_init
I just called muti_work_init for one times, why it was called for many times. I found some way which add freez_support in python code, but I dont know how to do in c++
I used multiprocessing in c++ , but it
s difference between c++ calling and python calling. my c++ code:
#includeinclude
include
include
include
//#include<opencv2/opencv.hpp>
include "opencv2/opencv.hpp"
include <numpy/arrayobject.h>
using namespace std; using namespace cv; int main(int argc, char argv[]) { printf("enter cpp main\n"); Py_Initialize(); if (!Py_IsInitialized()) return false; PyRun_SimpleString("import sys"); PyRun_SimpleString("sys.path.append('C:////Users////Administrator////Desktop////fpcb_interface////fpcb_interface////Detectron-master-mutiprocess')"); PyRun_SimpleString("sys.path.append('C:////Users////Administrator////Desktop////fpcb_interface////fpcb_interface////Detectron-master-mutiprocess////tools')"); //vs 的工作目录在 //Detection(); PyObject pModule = NULL; PyObject pFunc = NULL; pModule = PyImport_ImportModule("muti_infer"); //import infer_simple PyObject pDict = PyModule_GetDict(pModule); // PyObject *pyFunc = PyObject_GetAttrString(pModule, "muti_work_init"); //函数指针的入口地址 PyObject_CallObject(pyFunc, NULL); Py_Finalize(); getchar(); return 0; } ` my python file "muti_infer.py":
`from multiprocessing import Pipe from multiprocessing import Process from multiprocessing import freeze_support
def worker(): while True: print("subprocess !!!") pass
def muti_work_init(): w = Process(target = worker) w.start() print("finish muti_work_init")
if name == 'main': muti_work_init()`
This is consle output:
enter cpp main finish muti_work_init enter cpp main finish muti_work_init enter cpp main finish muti_work_init enter cpp main finish muti_work_init enter cpp main finish muti_work_init enter cpp main finish muti_work_init enter cpp main finish muti_work_init enter cpp main finish muti_work_init enter cpp main finish muti_work_init enter cpp main finish muti_work_init enter cpp main finish muti_work_init
I just called muti_work_init for one times, why it was called for many times. I found some way which add freez_support in python code, but I dont know how to do in c++