【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】
python下面的GIL决定了每次thread执行的时候不能实现完全的并发执行。所以如果多线程c调用python代码的时候,有很多地方需要注意一下。
// threads launched after everything is ok
PyEval_InitThreads();
PyEval_ReleaseThread(PyThreadState_Get());
Py_Initialize();
if (!Py_IsInitialized()) return -1;
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");
//import Module
pModule = PyImport_ImportModule("hello");
if (!pModule) {
printf("Can't import Module!/n");
return -1;
}
PyGILState_STATE gstate;
gstate = PyGILState_Ensure();
PyObject* pFun = PyObject_GetAttrString(pModule, "show");
PyObject_CallFunction(pFun, "s", param_name);
Py_DECREF(pFun);
PyGILState_Release(gstate);
PyGILState_Ensure();
Py_DECREF(pModule);
//Release
Py_Finalize();
#include
#include
#include
#include
#include
#include
int g_exit = 0;
PyObject* pModule;
void sig_process(int sig)
{
g_exit = 1;
}
void func(void* param_name)
{
while(!g_exit){
PyGILState_STATE gstate;
gstate = PyGILState_Ensure();
PyObject* pFun = PyObject_GetAttrString(pModule, "show");
PyObject_CallFunction(pFun, "s", param_name);
Py_DECREF(pFun);
PyGILState_Release(gstate);
}
}
int main()
{
pthread_t pid1, pid2, pid3;
signal(SIGINT, sig_process);
Py_Initialize();
if (!Py_IsInitialized()) return -1;
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");
//import Module
pModule = PyImport_ImportModule("hello");
if (!pModule) {
printf("Can't import Module!/n");
return -1;
}
// threads launched after everything is ok
PyEval_InitThreads();
PyEval_ReleaseThread(PyThreadState_Get());
if(pthread_create(&pid1, NULL, (void *(*)(void *))func, "output"))
{
return -1;
}
if(pthread_create(&pid2, NULL, (void *(*)(void *))func, "show"))
{
return -1;
}
if(pthread_create(&pid3, NULL, (void *(*)(void *))func, "see"))
{
return -1;
}
while(!g_exit){
sleep(3);
}
pthread_join(pid3, NULL);
pthread_join(pid2, NULL);
pthread_join(pid1, NULL);
PyGILState_Ensure();
Py_DECREF(pModule);
//Release
Py_Finalize();
return 0;
}
其中编译方法为,
gcc process.c -I/usr/include/python2.7 -ldl -lutil -lpthread -lpython2.7 -o process
而python脚本文件内容为,
def show(name):
print 'hello ' + name