尝试进行一个 config

This commit is contained in:
shenjack 2025-02-11 21:21:06 +08:00
parent 62a0a8d3fa
commit d94841b1bd
Signed by: shenjack
GPG Key ID: 7B1134A979775551
3 changed files with 58 additions and 7 deletions

View File

@ -42,7 +42,7 @@ colored = "3.0"
# runtime # runtime
tokio = { version = "1.43", features = ["full"] } tokio = { version = "1.43", features = ["full"] }
futures-util = "0.3.30" futures-util = "0.3.30"
pyo3 = { version = "0.23", features = ["experimental-async", "py-clone", "abi3-py38"] } pyo3 = { version = "0.23", features = ["experimental-async", "py-clone"] }
anyhow = { version = "1.0", features = ["backtrace"] } anyhow = { version = "1.0", features = ["backtrace"] }
# async 这玩意以后在搞 # async 这玩意以后在搞
# pyo3-async = "0.3.2" # pyo3-async = "0.3.2"

View File

@ -175,9 +175,7 @@ pub async fn failed_message(payload: Payload, _client: Client) {
} }
} }
pub async fn fetch_history(client: Client, room: RoomId) { pub async fn fetch_history(client: Client, room: RoomId) { let mut request_body = json!(room); }
let mut request_body = json!(room);
}
pub async fn fetch_messages(client: &Client, room: RoomId) { pub async fn fetch_messages(client: &Client, room: RoomId) {
let mut request_body = json!(room); let mut request_body = json!(room);

View File

@ -2,7 +2,7 @@ pub mod call;
pub mod class; pub mod class;
pub mod config; pub mod config;
use std::ffi::CString; use std::ffi::{CString, OsStr};
use std::fmt::Display; use std::fmt::Display;
use std::path::Path; use std::path::Path;
use std::sync::OnceLock; use std::sync::OnceLock;
@ -422,8 +422,61 @@ pub fn init_py() {
let plugin_path = MainStatus::global_config().py().plugin_path; let plugin_path = MainStatus::global_config().py().plugin_path;
event!(Level::INFO, "正在初始化 python"); // 根据 VIRTUAL_ENV 环境变量 进行一些处理
pyo3::prepare_freethreaded_python(); match std::env::var("VIRTUAL_ENV") {
Ok(virtual_env) => {
event!(Level::INFO, "找到 VIRTUAL_ENV 环境变量: {} 正在初始化", virtual_env);
// 使用 Py_InitializeFromConfig 初始化 python
unsafe {
let mut config = std::mem::zeroed::<pyo3::ffi::PyConfig>();
let config_ptr = &mut config as *mut pyo3::ffi::PyConfig;
// 初始化配置
// pyo3::ffi::PyConfig_InitIsolatedConfig(config_ptr);
pyo3::ffi::PyConfig_InitPythonConfig(config_ptr);
#[cfg(target_os = "linux")]
use std::os::unix::ffi::OsStrExt;
#[cfg(target_os = "windows")]
use std::os::windows::ffi::OsStrExt;
let wide_path = OsStr::new(&virtual_env).encode_wide().collect::<Vec<u16>>();
// 设置 prefix 和 exec_prefix
pyo3::ffi::PyConfig_SetString(
config_ptr,
&mut config.prefix as *mut _,
wide_path.as_ptr() as *mut _,
);
pyo3::ffi::PyConfig_SetString(
config_ptr,
&mut config.exec_prefix as *mut _,
wide_path.as_ptr() as *mut _,
);
// init py
let status = pyo3::ffi::Py_InitializeFromConfig(&config as *const _);
// 清理配置
pyo3::ffi::PyConfig_Clear(config_ptr);
match status._type {
pyo3::ffi::_PyStatus_TYPE::_PyStatus_TYPE_OK => {
event!(Level::INFO, "根据配置初始化 python 完成");
}
pyo3::ffi::_PyStatus_TYPE::_PyStatus_TYPE_EXIT => {
event!(Level::ERROR, "初始化 python 时发生错误: EXIT");
}
pyo3::ffi::_PyStatus_TYPE::_PyStatus_TYPE_ERROR => {
event!(Level::ERROR, "初始化 python 时发生错误: ERROR");
pyo3::ffi::Py_ExitStatusException(status);
}
}
}
}
Err(_) => {
event!(Level::INFO, "未找到 VIRTUAL_ENV 环境变量, 正常初始化");
pyo3::prepare_freethreaded_python();
event!(Level::INFO, "prepare_freethreaded_python 完成");
}
}
PyStatus::init(); PyStatus::init();
let plugin_path = PathBuf::from(plugin_path); let plugin_path = PathBuf::from(plugin_path);