When calling a linux binary which takes a relatively long time through Python's subprocess
module, does this release the GIL?
Yes, it releases the Global Interpreter Lock (GIL) in the calling process.
As you are likely aware, on POSIX platforms subprocess
offers convenience interfaces atop the "raw" components from fork
, execve
, and waitpid
.
By inspection of the CPython 2.7.9 sources, fork
and execve
do not release the GIL. However, those calls do not block, so we'd not expect the GIL to be released.
waitpid
of course does block, but we see it's implementation does give up 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 could also be tested by calling out to some long running program like sleep from a demonstration multithreaded python script.