15
15
16
16
#include " pyopenvino/core/common.hpp"
17
17
#include " pyopenvino/core/infer_request.hpp"
18
+ #include " pyopenvino/utils/utils.hpp"
18
19
19
20
namespace py = pybind11;
20
21
@@ -64,7 +65,7 @@ class AsyncInferQueue {
64
65
});
65
66
size_t idle_handle = m_idle_handles.front ();
66
67
// wait for request to make sure it returned from callback
67
- m_requests[idle_handle].m_request . wait ();
68
+ m_requests[idle_handle].m_request -> wait ();
68
69
if (m_errors.size () > 0 )
69
70
throw m_errors.front ();
70
71
return idle_handle;
@@ -75,7 +76,7 @@ class AsyncInferQueue {
75
76
// release GIL to avoid deadlock on python callback
76
77
py::gil_scoped_release release;
77
78
for (auto && request : m_requests) {
78
- request.m_request . wait ();
79
+ request.m_request -> wait ();
79
80
}
80
81
// acquire the mutex to access m_errors
81
82
std::lock_guard<std::mutex> lock (m_mutex);
@@ -87,7 +88,7 @@ class AsyncInferQueue {
87
88
for (size_t handle = 0 ; handle < m_requests.size (); handle++) {
88
89
// auto end_time = m_requests[handle].m_end_time; // TODO: pass it bellow? like in InferRequestWrapper
89
90
90
- m_requests[handle].m_request . set_callback ([this , handle /* ... */ ](std::exception_ptr exception_ptr) {
91
+ m_requests[handle].m_request -> set_callback ([this , handle /* ... */ ](std::exception_ptr exception_ptr) {
91
92
*m_requests[handle].m_end_time = Time::now ();
92
93
{
93
94
// acquire the mutex to access m_idle_handles
@@ -110,14 +111,17 @@ class AsyncInferQueue {
110
111
}
111
112
112
113
void set_custom_callbacks (py::function f_callback) {
114
+ // need to acquire GIL before py::function deletion
115
+ auto callback_sp = Common::utils::wrap_pyfunction (std::move (f_callback));
116
+
113
117
for (size_t handle = 0 ; handle < m_requests.size (); handle++) {
114
- m_requests[handle].m_request . set_callback ([this , f_callback , handle](std::exception_ptr exception_ptr) {
118
+ m_requests[handle].m_request -> set_callback ([this , callback_sp , handle](std::exception_ptr exception_ptr) {
115
119
*m_requests[handle].m_end_time = Time::now ();
116
120
if (exception_ptr == nullptr ) {
117
121
// Acquire GIL, execute Python function
118
122
py::gil_scoped_acquire acquire;
119
123
try {
120
- f_callback (m_requests[handle], m_user_ids[handle]);
124
+ (*callback_sp) (m_requests[handle], m_user_ids[handle]);
121
125
} catch (const py::error_already_set& py_error) {
122
126
// This should behave the same as assert(!PyErr_Occurred())
123
127
// since constructor for pybind11's error_already_set is
@@ -193,13 +197,13 @@ void regclass_AsyncInferQueue(py::module m) {
193
197
// Set new inputs label/id from user
194
198
self.m_user_ids [handle] = userdata;
195
199
// Update inputs if there are any
196
- self.m_requests [handle].m_request . set_input_tensor (inputs);
200
+ self.m_requests [handle].m_request -> set_input_tensor (inputs);
197
201
// Now GIL can be released - we are NOT working with Python objects in this block
198
202
{
199
203
py::gil_scoped_release release;
200
204
*self.m_requests [handle].m_start_time = Time::now ();
201
205
// Start InferRequest in asynchronus mode
202
- self.m_requests [handle].m_request . start_async ();
206
+ self.m_requests [handle].m_request -> start_async ();
203
207
}
204
208
},
205
209
py::arg (" inputs" ),
@@ -239,13 +243,13 @@ void regclass_AsyncInferQueue(py::module m) {
239
243
// Set new inputs label/id from user
240
244
self.m_user_ids [handle] = userdata;
241
245
// Update inputs if there are any
242
- Common::set_request_tensors (self.m_requests [handle].m_request , inputs);
246
+ Common::set_request_tensors (* self.m_requests [handle].m_request , inputs);
243
247
// Now GIL can be released - we are NOT working with Python objects in this block
244
248
{
245
249
py::gil_scoped_release release;
246
250
*self.m_requests [handle].m_start_time = Time::now ();
247
251
// Start InferRequest in asynchronus mode
248
- self.m_requests [handle].m_request . start_async ();
252
+ self.m_requests [handle].m_request -> start_async ();
249
253
}
250
254
},
251
255
py::arg (" inputs" ),
0 commit comments