Skip to content

Commit

Permalink
Merge pull request #1197 from luxonis/v3_python_callbacks_fix
Browse files Browse the repository at this point in the history
Acquire the GIL when calling Python callbacks in the MessageQueue
  • Loading branch information
moratom authored Dec 18, 2024
2 parents 2873abe + fd5cb64 commit e88ef08
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions bindings/python/src/MessageQueueBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,25 @@ void MessageQueueBindings::bind(pybind11::module& m, void* pCallstack) {

// Bind DataOutputQueue
auto addCallbackLambda = [](MessageQueue& q, py::function cb) -> int {
pybind11::module inspect_module = pybind11::module::import("inspect");
pybind11::object result = inspect_module.attr("signature")(cb).attr("parameters");
pybind11::module inspectModule = pybind11::module::import("inspect");
pybind11::object result = inspectModule.attr("signature")(cb).attr("parameters");
auto numParams = pybind11::len(result);
if(numParams == 2) {
return q.addCallback(cb.cast<std::function<void(std::string, std::shared_ptr<ADatatype>)>>());
} else if(numParams == 1) {
return q.addCallback(cb.cast<std::function<void(std::shared_ptr<ADatatype>)>>());
} else if(numParams == 0) {
return q.addCallback(cb.cast<std::function<void()>>());

if (numParams == 2) {
return q.addCallback([cb](std::string msg, std::shared_ptr<ADatatype> data) {
pybind11::gil_scoped_acquire gil;
cb(msg, data);
});
} else if (numParams == 1) {
return q.addCallback([cb](std::shared_ptr<ADatatype> data) {
pybind11::gil_scoped_acquire gil;
cb(data);
});
} else if (numParams == 0) {
return q.addCallback([cb]() {
pybind11::gil_scoped_acquire gil;
cb();
});
} else {
throw py::value_error("Callback must take either zero, one or two arguments");
}
Expand Down

0 comments on commit e88ef08

Please sign in to comment.