f2py函数释放GIL [英] f2py function release GIL
问题描述
当我调用f2py包装函数时,全局解释器锁(GIL)会被释放吗?
Does the Global Interpretter Lock (GIL) get released when I call an f2py wrapped function?
(我很乐意尝试自己发现,但我对这个numpy资源不熟悉,不知道该从哪里开始寻找)...
(I'm happy to try to discover on my own, but I'm not familiar enough with the numpy source to know where to start looking)...
为了澄清,对这个问题的一个很好的答案是要么帮助我知道在numpy源代码中寻找 Py_BEGIN_ALLOW_THREADS
或它只是让我知道GIL是否被释放(最好有一些证据)。
To clarify, a good answer to this question would either help me to know where in the numpy source to look for a Py_BEGIN_ALLOW_THREADS
or it would simply let me know if the GIL is released (preferably with some evidence).
推荐答案
不,f2py默认使用GIL。但是,您可以通过添加 threadsafe
指令来释放GIL。
No, f2py defaults to leaving the GIL in place. However, you can release the GIL by adding the threadsafe
directive.
示例:
subroutine foo(a)
!f2py threadsafe
!f2py intent(out) :: a
integer a
a = 5
end subroutine foo
现在编译它:
f2py -c -m foo --build-dir test_build foo.f90
我们可以检查源代码:
And we can check the source code:
grep THREAD test_build/src.*/*.c
build/src.linux-x86_64-2.7/testmodule.c: Py_BEGIN_ALLOW_THREADS
build/src.linux-x86_64-2.7/testmodule.c: Py_END_ALLOW_THREADS
但是,如果我们重复删除!f2py threadsafe
行的过程,那么不包括释放GIL的宏。
However, if we repeat the process removing the !f2py threadsafe
line, then the macros to release the GIL aren't included.
这篇关于f2py函数释放GIL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!