Skip to content
This repository was archived by the owner on Oct 23, 2023. It is now read-only.

Commit cafac66

Browse files
committed
Release the GIL
1 parent e6ffdcf commit cafac66

3 files changed

Lines changed: 55 additions & 16 deletions

File tree

cpp/frontend/frontend.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,6 @@ void Execution::notifyStart(std::function<void()> callback) {
204204
"Notify start " + description_);
205205
}
206206

207-
void Execution::getResult(std::function<void(Result)> callback) {
208-
getResult(std::move(callback), []() {});
209-
}
210-
211207
void Execution::getResult(std::function<void(Result)> callback,
212208
std::function<void()> errored) {
213209
auto promise = kj::newPromiseAndFulfiller<void>();
@@ -256,7 +252,9 @@ void Execution::getResult(std::function<void(Result)> callback,
256252
r.getResourceUsage().getStack();
257253
callback(result);
258254
},
259-
[errored](auto exc) { errored(); })
255+
[errored](auto exc) {
256+
if (errored) errored();
257+
})
260258
.eagerlyEvaluate(nullptr);
261259
},
262260
[fulfiller = ff](kj::Exception exc) {

cpp/frontend/frontend.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,8 @@ class Execution {
126126

127127
void notifyStart(std::function<void()> callback);
128128

129-
void getResult(std::function<void(Result)> callback);
130129
void getResult(std::function<void(Result)> callback,
131-
std::function<void()> errored);
130+
std::function<void()> errored = nullptr);
132131

133132
private:
134133
std::string description_;
@@ -142,6 +141,7 @@ class Execution {
142141

143142
class Frontend {
144143
friend class File;
144+
145145
public:
146146
Frontend(std::string server, int port);
147147

cpp/frontend/python/frontend.cpp

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,27 @@
77

88
#include "frontend/frontend.hpp"
99

10+
using namespace pybind11::literals;
11+
12+
template <typename T>
13+
class DestroyWithGIL {
14+
public:
15+
explicit DestroyWithGIL(T t) : t_(t) {}
16+
T& operator*() { return t_; }
17+
~DestroyWithGIL() {
18+
pybind11::gil_scoped_acquire acquire;
19+
t_ = T();
20+
}
21+
22+
private:
23+
T t_;
24+
};
25+
26+
template <typename T>
27+
DestroyWithGIL<T> destroy_with_gil(T t) {
28+
return DestroyWithGIL<T>(t);
29+
}
30+
1031
PYBIND11_MODULE(task_maker_frontend, m) {
1132
m.doc() = "Task-maker frontend module";
1233
pybind11::class_<frontend::Resources>(m, "Resources")
@@ -64,7 +85,13 @@ PYBIND11_MODULE(task_maker_frontend, m) {
6485

6586
pybind11::class_<frontend::File>(m, "File")
6687
.def("getContentsAsString",
67-
&frontend::File::getContentsAsString)
88+
[](frontend::File& f, std::function<void(std::string)> cb) {
89+
f.getContentsAsString(
90+
[cb = destroy_with_gil(cb)](std::string s) mutable {
91+
pybind11::gil_scoped_acquire acquire;
92+
(*cb)(s);
93+
});
94+
})
6895
.def("getContentsToFile", &frontend::File::getContentsToFile);
6996

7097
pybind11::class_<frontend::Execution>(m, "Execution")
@@ -82,21 +109,35 @@ PYBIND11_MODULE(task_maker_frontend, m) {
82109
pybind11::return_value_policy::reference)
83110
.def("output", &frontend::Execution::output,
84111
pybind11::return_value_policy::reference)
85-
.def("notifyStart", &frontend::Execution::notifyStart)
86-
.def("getResult", (void (frontend::Execution::*)(
87-
std::function<void(frontend::Result)>)) &
88-
frontend::Execution::getResult)
112+
.def("notifyStart",
113+
[](frontend::Execution& f, std::function<void()> cb) {
114+
f.notifyStart([cb = destroy_with_gil(cb)]() mutable {
115+
pybind11::gil_scoped_acquire acquire;
116+
(*cb)();
117+
});
118+
})
89119
.def("getResult",
90-
(void (frontend::Execution::*)(std::function<void(frontend::Result)>,
91-
std::function<void()>)) &
92-
frontend::Execution::getResult);
120+
[](frontend::Execution& f, std::function<void(frontend::Result)> cb,
121+
std::function<void()> err = nullptr) {
122+
f.getResult(
123+
[cb = destroy_with_gil(cb)](frontend::Result res) mutable {
124+
pybind11::gil_scoped_acquire acquire;
125+
(*cb)(res);
126+
},
127+
[err = destroy_with_gil(err)]() mutable {
128+
pybind11::gil_scoped_acquire acquire;
129+
if (*err) (*err)();
130+
});
131+
},
132+
"callback"_a, "error"_a = nullptr);
93133

94134
pybind11::class_<frontend::Frontend>(m, "Frontend")
95135
.def(pybind11::init<std::string, int>())
96136
.def("provideFile", &frontend::Frontend::provideFile,
97137
pybind11::return_value_policy::reference)
98138
.def("addExecution", &frontend::Frontend::addExecution,
99139
pybind11::return_value_policy::reference)
100-
.def("evaluate", &frontend::Frontend::evaluate)
140+
.def("evaluate", &frontend::Frontend::evaluate,
141+
pybind11::call_guard<pybind11::gil_scoped_release>())
101142
.def("stopEvaluation", &frontend::Frontend::stopEvaluation);
102143
}

0 commit comments

Comments
 (0)