[Pyrex] Py_BEGIN_ALLOW_THREADS
Phillip J. Eby
pje at telecommunity.com
Wed Jun 30 02:19:50 CEST 2004
At 08:34 PM 6/29/04 +0100, John J Lee wrote:
>On Tue, 29 Jun 2004, Simon Burton wrote:
>
> > On Mon, 28 Jun 2004 13:13:53 -0400
> > Itamar Shtull-Trauring <itamar at itamarst.org> wrote:
> >
> > > How would one use that in pyrex code?
> > >
> >
> > You have to expand the macro.
>
>By hand, you mean?
Yep.
> > Check out sec 8.1 in the Python/C API Reference Manual:
>[...]
>
>I think he knew that part ;-)
That *was* the expansion of the macros in question. IOW, to use BEGIN/END
allow threads, one would write something like:
cdef extern from "pythread.h"
ctypedef struct PyThreadState
PyThreadState *PyEval_SaveThread()
void PyEval_RestoreThread(PyThreadState *_save)
def code_that_releases_the_lock():
cdef PyThreadState *_save
_save = PyEval_SaveThread();
# do stuff here that *only* calls C code that
# doesn't call back to Python or the Python C API
PyEval_RestoreThread(_save);
Of course, this whole bit is somewhat complicated by the fact that the
BEGIN/END macros know what to do if the interpreter was built without
threads. You may have to make your own "mythreads.h" that does something like:
#ifdef HAVE_THREADS
#include "pythread.h"
#else
#define PyThreadState int
#define PyEval_SaveThread() NULL
#define PyEval_RestoreThread(_save) 0
#endif
I did something similar to this recently, in order to use the C equivalent
of threads.get_ident().
More information about the Pyrex
mailing list