Embedding python with a C++ multithread apps -> pause and resume python script

Sebastien Schertenleib sscherte at mailbox.epfl.ch
Tue Sep 10 04:25:36 EDT 2002


Hi,

I am developping a C++ application and I want to embedd python. I have succeeded to export my C++ classes and data using SWIG then I was able to create multithreading call to python and now I have my app running several script at the same time (~). However, I am stack as I would like to be able to pause and resume my python script from C++ side. 

If we imagine a looping script, my C++ thread in still in the Py_Run_SimpleString() method, and I would like to pause it inside python.

I have try to go directly inside the Interpreter state with all the thread queue but it seem that I don't understand how python access to data since I have set to NULL some thread state and my script continue to run as it.

If someone can give me a hint how to block a python script directly from C++, it would help me a lot.

A second problem is that when I try to redirect the sys.stderr and sys.stdout from python (create a python class which has a write method, and then which called inside my C++ method), It's working only until a certain time then it crash, how can I do a redirection in case of a c++ multithreading app? (I have synchronized the C++ method as well))

Thanks a lot for any help!
 

So, here is my thread run method:

/* Wait for the initialization thread if there is one.*/
if (_initScript != _PythonScriptHandle(NULL))

    _initScript->_thread->join();

PyEval_AcquireLock();

PyInterpreterState *interp = _service->_mainTstate->interp;

// create new PyThreadState only if needed

JTCThread * currentThread = JTCThread::currentThread();

PyThreadState * tstate;

vhdPythonServiceBody::_Impl::ThreadInfoMap::iterator it = _Impl::threadInfoMap.find(currentThread);

if (it == _Impl::threadInfoMap.end())

{

    vhdPythonServiceBody::_Impl::ThreadInfo threadInfo;

    threadInfo.pyThreadState = PyThreadState_New(interp);

    threadInfo.outputBuffer = "";

    it = vhdPythonServiceBody::_Impl::threadInfoMap.insert    (vhdPythonServiceBody::_Impl::ThreadInfoMap::value_type( currentThread, threadInfo)).first;

}

tstate = (*it).second.pyThreadState;

PyThreadState_Swap(tstate);


/* Execute the given script.*/

switch (PyRun_SimpleString((char*) _scriptText->cstr()))

{

case 0:

    _executionResult=RES_OK;

    break;

case -1:

    _executionResult=RES_ERROR;

    break;

}

PyThreadState_Swap(NULL);

// only for internal threads created by vhdPythonService (by using send for execution methods)if (_bServiceInternalThread)

{

    // Reset the thread state structure.

    PyThreadState_Clear(tstate);

    // Nullify the current thread state, release the global 

    // interpreter lock, delete the thread state structure.

    PyThreadState_Delete(tstate);

    // removal of ThreadInfo that was used for internal thread

    // we will not resues it as it was only for the internal thread

     _Impl::threadInfoMap.erase(currentThread);

}


PyEval_ReleaseLock();


/* Notify the service about the termination of the thread.

*/

_service->_notifyTermination(this);

}




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20020910/d9561698/attachment.html>


More information about the Python-list mailing list