所以,我想做的是在Python的递归函数中调用这个异步函数。但它不起作用,我不知道为什么它不应该。这是我在hpx中异步函数的绑定,并试图在python中的递归函数中使用
hpx::shared_future<int> myAsyncFunction(py::function f, py::args args)
{
return hpx::async([f, args]() {
py::gil_scoped_acquire acquire;
py::object result = f(*args);
return result.cast<int>();
});
}
import test
import pdb
test.init_hpx()
def fibonacci(n):
# pdb.set_trace()
if(n<2):
return n
n1 = test.new_async(fibonacci,n - 1)
n2 = test.new_async(fibonacci,n - 2)
if n1.is_ready() and n2.is_ready():
return n1.get() + n2.get()
print(fibonacci(10))
但它不工作,我得到这个错误
None
Fatal Python error: PyThreadState_Get: the function must be called with the GIL held, but the GIL is released (the current Python thread state is NULL)
Python runtime state: initialized
Thread 0x00007f508b7fe640 (most recent call first):
<no Python frame>
Thread 0x00007f509353a000 (most recent call first):
File "/home/matrix/pyhpx/python_hpx/build/fib.py", line 17 in <module>
我试着导入python.h并调用PyGILState_Ensure,但也不起作用
hpx::shared_future<int> myAsyncFunction(py::function f, py::args args) {
return hpx::async([f, args]() {
PyGILState_STATE gstate = PyGILState_Ensure(); // Acquire the GIL
py::object result = f(*args);
// Release the GIL
PyGILState_Release(gstate);
return result.cast<int>();
});
}
1条答案
按热度按时间pprl5pva1#
在调用
py::function f
时,您显然持有GIL。错误是在其他地方造成的。然而,很难判断在哪里,因为您只提供了代码的草图。