I have Windows 10, Visual Studio 2017 (Release 15.4), Python 3.6, Pybind11 version 2.2.4.
In order to achieve asynchronous execution of pybind11-wrapped C++ functions with normal python code, I have created a wrapper for std::future<int> via the following C++ code:
py::class_<std::future<int> >(moduleSystem, "FutureInt")
.def(py::init<>())
.def("get", &std::future<int>::get, "gets the value of the future for the task. this call is blocking, so waits until task is done");
Furthermore, I have define a simple C++ function which launches an asynchronous task via std::async. For simplicity, it just does let the current C++ thread sleep for a certain time (originally, the C++ function was writing out an image to a file asynchronously). The code for the C++ functions (the normal one, the async variant, and the pybind11 binder) is as follows:
int doSomeStuff()
{
std::this_thread::sleep_for(std::chrono::seconds(3));
return 0;
}
std::future<int> doSomeStuffAsync()
{
return std::async(std::launch::async, doSomeStuff);
}
PYBIND11_MODULE(WrapperModule, m)
{
m.def("dosSomeStuffAsync", &doSomeStuffAsync, "does some stuff asynchronously and returns a future for it");
}
In my python application, I am using this construct now in each iteration of a loop, in order run the C++ wrapped function 'doSomeStuff' in parallel to python code. So the sketch of my python application is as follows:
for k in range (0, 10):
# launch C++ function 'doSomeStuff' _asynchronously, and get the future for it
futureTask = doSomeStuffAsync()
# HERE, call some python code in _parallel_ to the C++ stuff. For simplicity, we do also here a sleep
time.sleep(1)
# now call 'future.get()' in order to wait for C++ function 'doSomeStuff' to finish
dummy = futureTask.get()
The problem I have now is that the Python applications hangs forever on the second loop iteration. It seems that it is hanging inside the 'futureTask.get()' function.
The behaviour seems to be also timing dependent, because e.g. if I put a breakpoint on the 'futureTask.get()' statement, it will work. It works also if I make the 'doSomeStuff' function do nothing, so the the asnychronous task is finished immediately.
What am I doing wrong ? I tried out different pybind11 return-policies in the wrapper of 'std::future.get' function, but didn't help.
Aucun commentaire:
Enregistrer un commentaire