Understanding PyEval_InitThreads
Gernot Hillier
ghillie at suse.de
Wed Nov 20 03:25:15 EST 2002
Hi!
Thomas Heller wrote:
> When porting an extension from Windows to Linux, I got crashes when
> C code calls back into Python.
>
> Here's what I'm doing:
>
> Py_BEGIN_ALLOW_THREAD
> call_some_c_code()
> Py_END_ALLOW_THREAD
>
> The call_some_c_code() function calls back into Python, so I have to
> acquire the interpreter lock again, and create a new threadstate in
> the callback. Note that g_interp is initialized when the extension
> module is loaded:
>
> static PyInterpreterState *g_interp;
>
> void call_back_into_python(void)
> {
> PyThreadState *pts;
>
> PyEval_AcquireLock();
> pts = PyThreadState_New(g_interp);
I can't answer your question exactly - but I personally would suggest to not
create a new thread state but doing it this way:
Py_BEGIN_ALLOW_THREAD
call_some_c_code(_save)
Py_END_ALLOW_THREAD
If you don't understand this, please note that Py_BEGIN_ALLOW_THREADS
expands to "{ PyThreadState *_save; _save = PyEval_SaveThread();".
call_some_c_code() has to pass the PyThreadState* to
call_back_into_python(). There you should do s.th. like this:
void call_back_into_python(PyThreadState *s)
{
PyEval_PyEval_RestoreThread(s)
// do your work
PyEval_SaveThread();
}
HTH...
--
Ciao,
Gernot
More information about the Python-list
mailing list