Shubham kumar
Shubham kumar

Reputation: 1

How to use pybind11 to call hpx async function in recursive python code?

So , what I want to do is to call this async function in the recursive function in python . But it is not working and I dont have idea why it shouldn't . This is my binding for async function in hpx and trying to use in recursive function in 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))

but its not working and i am getting this error

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>

I tried inporting python.h and calling PyGILState_Ensure thats also not working

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>();
    });
}

Upvotes: 0

Views: 160

Answers (1)

hkaiser
hkaiser

Reputation: 11521

You clearly hold the GIL while invoking the py::function f. The error is caused in some other place. It is however difficult to tell where, as you have provided only a sketch of the code.

Upvotes: 0

Related Questions