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+
1031PYBIND11_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