[Python-Dev] Extension modules, Threading, and the GIL
"Martin v. Löwis"
martin@v.loewis.de
Wed, 08 Jan 2003 15:49:45 +0100
Mark Hammond wrote:
> It can be done, yes. I am not looking for a change in semantics, just a
> simple way to do it (and maybe even a fast way to do it, but that is
> secondary). If such a way already exists, please enlighten us. If not, but
> it is sufficiently simple to describe, then please describe it. Otherwise,
> I do not understand your point.
There is a very simple strategy to support multiple threads in an
extension module.
1. In all callbacks, create a thread state and acquire the current
thread (this requires a singleton interpreter state).
2. In all API calls that may invoke callbacks, use
Py_BEGIN_ALLOW_THREADS and Py_END_ALLOW_THREADS around the API call.
If this strategy is followed, every code always has all Python
resources, and no deadlocks result.
Regards,
Martin