Manipulate GIL to have Python thread work with pthread native ones

Lawrence D’Oliveiro lawrencedo99 at gmail.com
Wed Jun 1 20:29:08 EDT 2016


On Tuesday, May 31, 2016 at 10:14:31 PM UTC+12, qsh... at alumni.sjtu.edu.cn wrote:
> PyGILState_Ensure/PyGILState_Release. Without a running sample, it is a
> little bit hard to understand how Python thread and the native pthread
> interact. 

Python threads *are* native threads (at least on Linux).

The place to release the GIL is where you are a) not making any Python API calls and b) doing something CPU-intensive. Point a) is essential.

Here <https://github.com/ldo/grainypy/blob/master/grainyx.c> is one of my example extension modules. It doesn’t actually do any multithreading, but I put in the Py_{BEGIN,END}_ALLOW_THREADS calls where I thought it made sense to have them anyway.


More information about the Python-list mailing list