0

Can i not send and receive UDP packets in a single python embedded C++ program in which send and receive scripts run in different threads? I get an unhandled exception when the exe file is run. When one of the send or receive command is commented out i.e either PyRun_SimpleString(sendPy) or PyRun_SimpleString(recPy) then the program works all fine. What is the mistake here?

The code is given below:

DWORD WINAPI sendPack(LPVOID iValue)
{


while(1){

   const char* sendPy = "UDPSockSend.sendto('10707',('10.107.35.167',21567))";
   PyRun_SimpleString(sendPy);

    }
return 0;
}


DWORD WINAPI receive(LPVOID iValue){


while(1){

    Py_Initialize();
    recPy = "data,addr = UDPSockRcv.recvfrom(99000)";          
    PyRun_SimpleString(recPy);

}

return 0;

}

int threads()    
{

    HANDLE sendPackThread, receiveThread; 
    DWORD dwGenericThread;     

    char lszThreadParam[4]; 

    receiveThread = 
    CreateThread(NULL,0,receive,&lszThreadParam,0,&dwGenericThread);          
    if(receiveThread == NULL){           
        DWORD dwError = GetLastError();           
        return 0;     
    }



    sendPackThread =     
    CreateThread(NULL,0,sendPack,&lszThreadParam,0,&dwGenericThread);     
        if(sendPackThread == NULL){           
        DWORD dwError = GetLastError();           
        std::cout<<"SCM:Error in Creating send sample thread"<<dwError<<"\n" ;       
        return 0;     
        }

    return 1;
}


int main(int argc, char* argv[])
{

    using namespace std;
    Py_Initialize();

    const char * initPy = "import socket; 
    UDPSockSend = socket.socket(socket.AF_INET,socket.SOCK_DGRAM); 
    UDPSockRcv = socket.socket(socket.AF_INET,socket.SOCK_DGRAM);
    listen_addr = ('',2000);UDPSockRcv.bind(listen_addr)";
    PyRun_SimpleString(initPy);


    int thd = threads();

    system("pause");
    return 0;

}

Thanks in advance

4

1 回答 1

1

我认为这里的问题是,python 不是线程安全的。您不能只从两个线程访问解释器并期望它可以工作。有关更多详细信息,请参阅http://docs.python.org/2/c-api/init.html#non-python-created-threads。本质上,您必须获取和释放 GIL(全局解释器锁)。

这个东西是一种互斥锁,可以确保一次只有一个线程访问 python 对象。GIL也是python多线程性能普遍不好的原因。

于 2013-05-10T13:25:57.210 回答