f2py function release GIL f2py function release GIL numpy numpy

f2py function release GIL


No, f2py defaults to leaving the GIL in place. However, you can release the GIL by adding the threadsafe directive.

example:

subroutine foo(a)!f2py threadsafe!f2py intent(out) :: ainteger aa = 5end subroutine foo

Now compile it:

f2py -c -m foo --build-dir test_build foo.f90

And we can check the source code:

grep THREAD test_build/src.*/*.cbuild/src.linux-x86_64-2.7/testmodule.c:      Py_BEGIN_ALLOW_THREADSbuild/src.linux-x86_64-2.7/testmodule.c:      Py_END_ALLOW_THREADS

However, if we repeat the process removing the !f2py threadsafe line, then the macros to release the GIL aren't included.