From dd1e66da6f4c29b5bf5d81d8d229dfdd8b4dd9ec Mon Sep 17 00:00:00 2001 From: xia-chu <771730766@qq.com> Date: Tue, 2 Dec 2025 20:48:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E7=9B=B8=E5=85=B3python=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python/mk_plugin.py | 7 ++++++- server/pyinvoker.cpp | 49 ++++++++++++++++++++++++++++++++++++-------- server/pyinvoker.h | 2 ++ 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/python/mk_plugin.py b/python/mk_plugin.py index 4e471f6f..01289776 100644 --- a/python/mk_plugin.py +++ b/python/mk_plugin.py @@ -2,7 +2,9 @@ import mk_logger import mk_loader def on_start(): - mk_logger.log_info("on_start") + mk_logger.log_info(f"on_start, secret: {mk_loader.get_config('api.secret')}") + mk_loader.set_config('api.secret', "new_secret_from_python") + mk_loader.update_config() def on_exit(): mk_logger.log_info("on_exit") @@ -29,3 +31,6 @@ def on_flow_report(args: dict, totalBytes: int, totalDuration: int, isPlayer: bo mk_logger.log_info(f"args: {args}, totalBytes: {totalBytes}, totalDuration: {totalDuration}, isPlayer: {isPlayer}, sender: {sender}") # 返回True代表此事件被python拦截 return True + +def on_reload_config(): + mk_logger.log_info(f"on_reload_config") \ No newline at end of file diff --git a/server/pyinvoker.cpp b/server/pyinvoker.cpp index 831a473c..efc22c69 100644 --- a/server/pyinvoker.cpp +++ b/server/pyinvoker.cpp @@ -12,6 +12,10 @@ using namespace toolkit; using namespace mediakit; +extern ArgsType make_json(const MediaInfo &args); +extern void fillSockInfo(Json::Value & val, SockInfo* info); +extern std::string g_ini_file; + template typename std::enable_if::value, py::capsule>::type to_python(const T &obj) { static auto name_str = toolkit::demangle(typeid(T).name()); @@ -34,9 +38,6 @@ typename std::enable_if::value, py::capsule>::typ }); } -extern ArgsType make_json(const MediaInfo &args); -extern void fillSockInfo(Json::Value & val, SockInfo* info); - static py::dict jsonToPython(const Json::Value &obj) { py::dict ret; if (obj.isObject()) { @@ -91,6 +92,26 @@ PYBIND11_EMBEDDED_MODULE(mk_loader, m) { py::gil_scoped_release release; LoggerWrapper::printLog(::toolkit::getLogger(), lev, file, func, line, content); }); + + m.def("get_config", [](const std::string &key) -> std::string { + py::gil_scoped_release release; + const auto it = mINI::Instance().find(key); + if (it != mINI::Instance().end()) { + return it->second; + } + return ""; + }); + m.def("set_config", [](const std::string &key, const std::string &value) -> bool { + py::gil_scoped_release release; + mINI::Instance()[key]= value; + return true; + }); + m.def("update_config", []() { + NOTICE_EMIT(BroadcastReloadConfigArgs, Broadcast::kBroadcastReloadConfig); + mINI::Instance().dumpFile(g_ini_file); + return true; + }); + m.def("publish_auth_invoker_do", [](const py::capsule &cap, const std::string &err, const py::dict &opt) { ProtocolOption option; option.load(to_native(opt)); @@ -145,9 +166,16 @@ PythonInvoker::PythonInvoker() { set_python_path(); // 确保 PYTHONPATH 在第一次调用时设置 _interpreter = new py::scoped_interpreter; _rel = new py::gil_scoped_release; + + NoticeCenter::Instance().addListener(this, Broadcast::kBroadcastReloadConfig, [this] (BroadcastReloadConfigArgs) { + if (_on_reload_config) { + _on_reload_config(); + } + }); } PythonInvoker::~PythonInvoker() { + NoticeCenter::Instance().delListener(this, Broadcast::kBroadcastReloadConfig); { py::gil_scoped_acquire gil; // 加锁 if (_on_exit) { @@ -166,12 +194,6 @@ void PythonInvoker::load(const std::string &module_name) { try { py::gil_scoped_acquire gil; // 加锁 _module = py::module::import(module_name.c_str()); - if (hasattr(_module, "on_start")) { - py::object on_start = _module.attr("on_start"); - if (on_start) { - on_start(); - } - } if (hasattr(_module, "on_exit")) { _on_exit = _module.attr("on_exit"); } @@ -184,6 +206,15 @@ void PythonInvoker::load(const std::string &module_name) { if (hasattr(_module, "on_flow_report")) { _on_flow_report = _module.attr("on_flow_report"); } + if (hasattr(_module, "on_reload_config")) { + _on_reload_config = _module.attr("on_reload_config"); + } + if (hasattr(_module, "on_start")) { + py::object on_start = _module.attr("on_start"); + if (on_start) { + on_start(); + } + } } catch (py::error_already_set &e) { PrintE("Python exception:%s", e.what()); } diff --git a/server/pyinvoker.h b/server/pyinvoker.h index b8d47e3e..9b9741fb 100644 --- a/server/pyinvoker.h +++ b/server/pyinvoker.h @@ -44,6 +44,8 @@ private: py::object _on_play; // 流量汇报接口 py::object _on_flow_report; + // 配置文件热更新回调 + py::object _on_reload_config; }; } // namespace mediakit