Embedded Python in C++ socket programming -
can not send , receive udp packets in single python embedded c++ program in send , receive scripts run in different threads? unhandled exception when exe file run. when 1 of send or receive command commented out i.e either pyrun_simplestring(sendpy) or pyrun_simplestring(recpy) program works fine. mistake here?
the code 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
i think problem here is, python not thread safe. can't access interpreter 2 threads , expect works. see http://docs.python.org/2/c-api/init.html#non-python-created-threads more details. have acquire , release gil (the global interpreter lock).
this thing kind of mutex ensures, 1 thread accesses python objects @ time. gil reason why python multithread performance bad.
Comments
Post a Comment