Understanding PyEval_InitThreads

Thomas Heller theller at python.net
Wed Nov 20 05:32:01 EST 2002


Gernot Hillier <ghillie at suse.de> writes:

> 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();
> }

Unfortunately it won't work this way.

Think of call_some_c_code() being the C standard library's qsort
function, and call_back_into_python() is the function pointer I gave
qsort to do the comparison. There's no way to pass the thread state to
the callback function!

Storing the thread state in a global variable would be possible, but
in this case it wouldn't be thread safe, I assume, and *only* work if
the callout into C is done by my extension module.

Thanks,

Thomas



More information about the Python-list mailing list