[Pyrex] GIL question

Phillip J. Eby pje at telecommunity.com
Wed Oct 6 20:51:44 CEST 2004


At 11:36 AM 10/6/04 -0700, Cory Dodt wrote:

>Does Pyrex provide any way to release the GIL?


Have you tried this:


cdef extern from "Python.h"
     struct PyThreadState
     PyThreadState* PyEval_SaveThread()
     void PyEval_RestoreThread(PyThreadState *tstate)


def do_something():

     cdef PyThreadState *_save
     _save = PyEval_SaveThread()

     # ...call some pure C function that doesn't use Python objects...

     PyEval_RestoreThread(_save)


I pretty much translated the above straight from "8.1 Thread State and the 
Global Interpreter Lock" in the Python/C API manual.  Note, however, that 
you should only invoke the functions if thread support is present.  (#ifdef 
WITH_THREAD), so you may need to actually use a different header file, with 
something like this in it:

#ifndef WITH_THREAD
#define PyEval_SaveThread() NULL
#define PyEval_RestoreThread(x)
#endif

At least, if you want to support non-threaded Pythons.





More information about the Pyrex mailing list