python编程(多线程c回调python)

【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】

    python下面的GIL决定了每次thread执行的时候不能实现完全的并发执行。所以如果多线程c调用python代码的时候,有很多地方需要注意一下。

1、开始处添加多线程支持
    // threads launched after everything is ok
    PyEval_InitThreads();  
2、在子线程创建之前,主线程释放控制权
    PyEval_ReleaseThread(PyThreadState_Get());  
3、子线程创建之前必须完成其他所有操作
    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;
    }  
4、子线程调用python的时候先用锁保护好
        PyGILState_STATE gstate;
        gstate = PyGILState_Ensure();

        PyObject* pFun = PyObject_GetAttrString(pModule, "show");   
        PyObject_CallFunction(pFun, "s", param_name);
        Py_DECREF(pFun);
        PyGILState_Release(gstate);
5、子线程结束后交还给主线程时,最好再次锁住虚拟机
    PyGILState_Ensure(); 
    Py_DECREF(pModule);  

    //Release   
    Py_Finalize();
6、完整流程如下所示,
#include <stdio.h>  
#include <pthread.h>  
#include <unistd.h>  
#include <stdlib.h>  
#include <signal.h>
#include <Python.h>

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 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嵌入式-老费

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值