[SciPy-user] Multithreading cookbook entry

Thomas Heller theller@ctypes....
Thu Feb 21 15:27:09 CST 2008

Anne Archibald schrieb:

> In general, the answer is that if a C extension can function outside
> the GIL, it has to explicitly release it. TBH, I'm not sure what it
> has to do first to make sure the interpreter is in a safe state -
> maybe nothing - but it has to explicitly declare that it's not going
> to modify any interpreter state.
> Many numpy functions - exp is obviously an example - do this. Others
> don't. It would be useful to go through the code looking at which ones
> do and don't release the GIL, and put it in their docstrings; it might
> be possible to make more release the GIL. It's a pretty safe bet that
> the ufuncs do; I would guess that the linear algebra functions do too.
> Probably not much else.
> If an extension uses ctypes, whether it releases the GIL is up to
> ctypes. I would guess that it doesn't, since ctypes knows nothing
> about the C function, but I have never actually used ctypes.

Of course does ctypes release the GIL on foreign function calls.  And the GIL
is acquired if Python implemented callback functions call back into
Python code.

There is nothing that ctypes needs to know about the C function - if the
C function is not thread safe, you must not call it from other threads.

Except - if the C function makes Python api calls, however, the GIL must not be
released.  In this case you should use the Python calling convention; for details
look up the docs (pydll and such).

This is even documented ;-)


More information about the SciPy-user mailing list