完成配置文件相关python接口

This commit is contained in:
xia-chu
2025-12-02 20:48:10 +08:00
parent 566761b47e
commit dd1e66da6f
3 changed files with 48 additions and 10 deletions

View File

@@ -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")

View File

@@ -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 T>
typename std::enable_if<std::is_copy_constructible<T>::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<!std::is_copy_constructible<T>::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());
}

View File

@@ -44,6 +44,8 @@ private:
py::object _on_play;
// 流量汇报接口
py::object _on_flow_report;
// 配置文件热更新回调
py::object _on_reload_config;
};
} // namespace mediakit