When invoking a linux binary that takes a relatively long time through the Python subprocess module, does it release the GIL?
Yes, it releases the Global Interpreter Lock (GIL) in the calling process.
As you know, on POSIX platforms, subprocess offers convenient interfaces on top of the raw components from fork , execve and waitpid .
By checking the sources of CPython 2.7.9, fork and execve do not release the GIL. However, these calls are not blocked, so we do not expect the GIL to be released.
waitpid , of course, blocks, but we see that the implementation rejects the GIL using the ALLOW_THREADS macros:
static PyObject * posix_waitpid(PyObject *self, PyObject *args) { .... Py_BEGIN_ALLOW_THREADS pid = waitpid(pid, &status, options); Py_END_ALLOW_THREADS ....
This can also be tested by invoking some long program like sleep from a demo multi-threaded python script.
pilcrow
source share