Serializing PyThreads without holding interpreter lock?

Martin v. Löwis martin.vonloewis at hpi.uni-potsdam.de
Mon Mar 17 11:25:01 EST 2003


> At first sight, a solution to this problem seems to be releasing the
> global interpreter lock, by
> 1) adding Py_BEGIN_UNBLOCK_THREADS and Py_BEGIN_BLOCK_THREADS
> around the  call to the solver, and

You should use Py_BEGIN_ALLOW_THREADS and Py_END_ALLOW_THREADS
here.

> The question is now how to prevent this from happening, i.e. how do I
> serialize calls to the solver without holding the global interpreter lock?

I recommend to put another lock around the solver code. See _tkinter.c
for an example; it also puts Tcl into a lock. This requires a few additional
macros, since you need a state where you hold both locks, and a state
where you just hold the solver lock.

> The Python C programming manuals do give a number of primitives for
> handling thread, but blocking threads is not part of the provided
> functionality.

That's not true. If you allocate additional locks, waiting for these locks
will block the calling thread if another thread already holds the lock.

HTH,
Martin






More information about the Python-list mailing list