For Controlling Python Threads switch from a C/C++ application

Nisamudeen Nisamudeen at BNS-solutions.com
Thu Apr 17 08:12:52 EDT 2003


Hi ,

i have embeded a python(Multithreaded for executing different script file in
each thread ) in a c++ application,  Its working properly but the problem is
, 
I want to control the thread switching from my thread (main thread ) .. Is
it possible ?? help me .. 

Case : 

/// class for the multiple thread state 
class PythonInterpreter
{
public:
	PythonInterpreter()
		: _threadState(0)
	{
		PyEval_AcquireLock();
		_threadState = Py_NewInterpreter();
		assert(_threadState);
		PyThreadState_Swap(_threadState);

	}
	~PythonInterpreter()
	{
		Py_EndInterpreter(_threadState);
		PyEval_ReleaseLock();
	}
public:
	PyThreadState* _threadState;
}; 

// Thread  //////////////////
DWORD _stdcall CheckKey( void *dummy )   
{
	char * filename = (char*) dummy ; 

	PythonInterpreter opython ;  

	pfobj = (PyFileObject * )PyFile_FromString( filename, "r+") ; 

	PyRun_SimpleFile( pfobj->f_fp   , filename  ) ;
	return 0 ;
}



int main(int argc, char *argv[])
{
	char filename1[50],filename2[50] ; 
	DWORD dwID ; 
	HANDLE hHand ,hHand1 ;  

	Py_Initialize();
	PyEval_InitThreads(); // initalize threading
	PyThreadState* state = PyEval_SaveThread(); // release lock 


	strcpy(filename1 ,"c:\\ONE.py"); 
	strcpy(filename2 ,"c:\\TWO.py"); 
 
	

	hHand1 =  CreateThread( NULL , 0, CheckKey , (void *)filename1 , 0,
&dwID ) ;
	printf("Started One ") ;

	Sleep(1000) ; 
	hHand =  CreateThread( NULL , 0, CheckKey , (void *)filename2 , 0,
&dwID ) ;

	Sleep(10000) ;
	Py_Exit(0); 
	Py_Finalize();
	PyEval_RestoreThread(state);
	return 0;
}


its actully working well , its also switching between thread (python taking
care of switching), but what i want is from the main() thread , i want to
suspend or resume each of the thread based on my requrement , for eg:- i
want to execute #1 thread for 100 mill sec and after that , i want to switch
to #2 for another 100 m sec  ,like wise , is it possible ????



Nisamudeen S






More information about the Python-list mailing list