From 56e2bc2c4774ec6e6f3213285db80d7afa49cd26 Mon Sep 17 00:00:00 2001 From: Stephen Shelton Date: Mon, 6 Jul 2020 17:40:42 -0600 Subject: [PATCH] Acquire python's Global Interpreter Lock in callbacks --- pybind/llarp/tooling/router_hive.cpp | 30 +++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/pybind/llarp/tooling/router_hive.cpp b/pybind/llarp/tooling/router_hive.cpp index 86156f5dc..b5c23d825 100644 --- a/pybind/llarp/tooling/router_hive.cpp +++ b/pybind/llarp/tooling/router_hive.cpp @@ -12,6 +12,9 @@ namespace tooling RouterHive_Init(py::module& mod) { using RouterHive_ptr = std::shared_ptr; + using Context_ptr = RouterHive::Context_ptr; + using ContextVisitor = std::function; + py::class_(mod, "RouterHive") .def(py::init<>()) .def("AddRelay", &RouterHive::AddRelay) @@ -19,9 +22,30 @@ namespace tooling .def("StartRelays", &RouterHive::StartRelays) .def("StartClients", &RouterHive::StartClients) .def("StopAll", &RouterHive::StopRouters) - .def("ForEachRelay", &RouterHive::ForEachRelay) - .def("ForEachClient", &RouterHive::ForEachClient) - .def("ForEachRouter", &RouterHive::ForEachRouter) + .def( + "ForEachRelay", + [](RouterHive& hive, ContextVisitor visit) { + hive.ForEachRelay([visit](Context_ptr ctx) { + py::gil_scoped_acquire acquire; + visit(std::move(ctx)); + }); + }) + .def( + "ForEachClient", + [](RouterHive& hive, ContextVisitor visit) { + hive.ForEachClient([visit](Context_ptr ctx) { + py::gil_scoped_acquire acquire; + visit(std::move(ctx)); + }); + }) + .def( + "ForEachRouter", + [](RouterHive& hive, ContextVisitor visit) { + hive.ForEachRouter([visit](Context_ptr ctx) { + py::gil_scoped_acquire acquire; + visit(std::move(ctx)); + }); + }) .def("GetNextEvent", &RouterHive::GetNextEvent) .def("GetAllEvents", &RouterHive::GetAllEvents) .def("RelayConnectedRelays", &RouterHive::RelayConnectedRelays)