MCPcopy Create free account
hub / github.com/boostorg/cobalt / await_suspend

Method await_suspend

example/python.cpp:219–263  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

217
218 template<typename T>
219 bool await_suspend(std::coroutine_handle<T> h)
220 {
221 asio::any_io_executor exec;
222 if constexpr (requires (T & p) {p.get_executor();})
223 exec = h.promise().get_executor();
224 else
225 exec = cobalt::this_thread::get_executor();
226
227 auto loop = get_loop();
228 auto task = getattr(loop, "create_task")(target);
229
230 struct wrapper
231 {
232 asio::any_io_executor exec;
233 mutable cobalt::unique_handle<void> awaiter;
234 py_awaitable * res;
235
236 void operator()(py::object t) const
237 {
238 res->extract_result(t);
239 asio::dispatch(exec, std::move(awaiter));
240 }
241 };
242
243 if (py::cast<bool>(getattr(task, "done")()))
244 {
245 extract_result(task);
246 return false;
247 }
248
249 getattr(task, "add_done_callback")(py::cpp_function(wrapper{
250 std::move(exec),
251 cobalt::unique_handle<void>{h.address()},
252 this
253 }));
254
255 if constexpr (requires (T & p) {p.get_cancellation_slot();})
256 h.promise().get_cancellation_slot().
257 assign([c = getattr(task, "cancel")](asio::cancellation_type ct) {
258 py::gil_scoped_acquire lock;
259 c();
260 });
261
262 return true;
263 }
264
265 void extract_result(py::object task)
266 {

Callers

nothing calls this directly

Calls 7

get_executorFunction · 0.85
get_loopFunction · 0.85
get_executorMethod · 0.45
promiseMethod · 0.45
addressMethod · 0.45
get_cancellation_slotMethod · 0.45
assignMethod · 0.45

Tested by

no test coverage detected