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

1 Answer 1

1

I think the problem here is, that python is not thread safe. You can't just access the interpreter from two threads and expect that it works. See http://docs.python.org/2/c-api/init.html#non-python-created-threads for more details. Essentially you have to acquire and release the GIL (The global interpreter lock).

This thing is kind of a mutex that ensures, that only one Thread accesses python objects at a time. The GIL is also the reason why python multithread performance is generally bad.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.