防止开启Python插件后,程序退出时异常崩溃
Some checks failed
Android / build (push) Has been cancelled
CodeQL / Analyze (cpp) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Docker / build (push) Has been cancelled
DockerPy / build (push) Has been cancelled
Linux / build (push) Has been cancelled
Linux_Python / build (push) Has been cancelled
macOS / build (push) Has been cancelled
macOS_Python / build (push) Has been cancelled
Windows / build (push) Has been cancelled
Windows_Python / build (push) Has been cancelled

This commit is contained in:
xia-chu 2026-05-04 10:49:08 +08:00
parent 3aa9b8977c
commit 427bb56507

View File

@ -24,6 +24,7 @@ extern ArgsType make_json(const MediaInfo &args);
extern void fillSockInfo(Json::Value & val, SockInfo* info); extern void fillSockInfo(Json::Value & val, SockInfo* info);
extern ArgsType getRecordInfo(const RecordInfo &info); extern ArgsType getRecordInfo(const RecordInfo &info);
extern std::string g_ini_file; extern std::string g_ini_file;
static std::shared_ptr<PythonInvoker> g_instance;
template <typename T> template <typename T>
typename std::enable_if<std::is_copy_constructible<T>::value, py::capsule>::type to_python(const T &obj) { typename std::enable_if<std::is_copy_constructible<T>::value, py::capsule>::type to_python(const T &obj) {
@ -388,11 +389,28 @@ PYBIND11_EMBEDDED_MODULE(mk_loader, m) {
py::arg("opt") = py::dict() py::arg("opt") = py::dict()
); );
m.def("set_fastapi", [](const py::object &check_route, const py::object &submit_coro) { m.def("set_fastapi", [](const py::function &check_route, const py::function &submit_coro) {
static void *fastapi_tag = nullptr; static void *fastapi_tag = nullptr;
NoticeCenter::Instance().delListener(&fastapi_tag, Broadcast::kBroadcastHttpRequest); NoticeCenter::Instance().delListener(&fastapi_tag, Broadcast::kBroadcastHttpRequest);
NoticeCenter::Instance().addListener(&fastapi_tag, Broadcast::kBroadcastHttpRequest, [check_route, submit_coro](BroadcastHttpRequestArgs) { struct py_context {
handle_http_request(check_route, submit_coro, parser, invoker, consumed, sender); py::function check_route;
py::function submit_coro;
std::shared_ptr<PythonInvoker> invoker;
~py_context() {
{
py::gil_scoped_acquire guard;
check_route = py::function { };
submit_coro = py::function { };
}
invoker = nullptr;
};
};
auto ptr = std::make_shared<py_context>();
ptr->check_route = check_route;
ptr->submit_coro = submit_coro;
ptr->invoker = g_instance;
NoticeCenter::Instance().addListener(&fastapi_tag, Broadcast::kBroadcastHttpRequest, [ptr](BroadcastHttpRequestArgs) {
handle_http_request(ptr->check_route, ptr->submit_coro, parser, invoker, consumed, sender);
}); });
}); });
@ -570,8 +588,6 @@ bool set_python_path() {
return true; return true;
} }
static std::shared_ptr<PythonInvoker> g_instance;
PythonInvoker &PythonInvoker::Instance() { PythonInvoker &PythonInvoker::Instance() {
static toolkit::onceToken s_token([]() { static toolkit::onceToken s_token([]() {
g_instance.reset(new PythonInvoker); g_instance.reset(new PythonInvoker);