From 4866f2ec2e9d985b8f7b187e6aa9e156ffbdc8cc Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Tue, 11 Feb 2025 23:25:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=88=91=E6=82=9F=E4=BA=86=EF=BC=8C=E5=BF=98?= =?UTF-8?q?=E8=AE=B0=20save=20thread=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ica-rs/src/ica.rs | 3 ++- ica-rs/src/py/call.rs | 6 +++--- ica-rs/src/py/mod.rs | 13 +++++++++++-- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/ica-rs/src/ica.rs b/ica-rs/src/ica.rs index f8a0beb..8bfb289 100644 --- a/ica-rs/src/ica.rs +++ b/ica-rs/src/ica.rs @@ -47,6 +47,7 @@ pub async fn start_ica(config: &IcaConfig, stop_reciver: StopGetter) -> ClientRe event!(Level::INFO, "ica-async-rs v{} initing", crate::ICA_VERSION); + let start_connect_time = std::time::Instant::now(); let socket = match ClientBuilder::new(config.host.clone()) .transport_type(TransportType::Websocket) .on_any(async_any_callback!(events::any_event)) @@ -65,7 +66,7 @@ pub async fn start_ica(config: &IcaConfig, stop_reciver: StopGetter) -> ClientRe .await { Ok(client) => { - event!(Level::INFO, "socketio connected"); + event!(Level::INFO, "socketio connected time: {:?}", start_connect_time.elapsed()); client } Err(e) => { diff --git a/ica-rs/src/py/call.rs b/ica-rs/src/py/call.rs index a968077..3a03992 100644 --- a/ica-rs/src/py/call.rs +++ b/ica-rs/src/py/call.rs @@ -224,7 +224,7 @@ pub async fn ica_new_message_py(message: &ica::messages::NewMessage, client: &Cl let client = class::ica::IcaClientPy::new(client); let args = (msg, client); let task = call_py_func!(args, plugin, path, ICA_NEW_MESSAGE_FUNC, client); - // PY_TASKS.lock().await.push_ica_new_message(task); + PY_TASKS.lock().await.push_ica_new_message(task); } } @@ -237,7 +237,7 @@ pub async fn ica_delete_message_py(msg_id: ica::MessageId, client: &Client) { let client = class::ica::IcaClientPy::new(client); let args = (msg_id.clone(), client); let task = call_py_func!(args, plugin, path, ICA_DELETE_MESSAGE_FUNC, client); - // PY_TASKS.lock().await.push_ica_delete_message(task); + PY_TASKS.lock().await.push_ica_delete_message(task); } } @@ -253,6 +253,6 @@ pub async fn tailchat_new_message_py( let client = class::tailchat::TailchatClientPy::new(client); let args = (msg, client); let task = call_py_func!(args, plugin, path, TAILCHAT_NEW_MESSAGE_FUNC, client); - // PY_TASKS.lock().await.push_tailchat_new_message(task); + PY_TASKS.lock().await.push_tailchat_new_message(task); } } diff --git a/ica-rs/src/py/mod.rs b/ica-rs/src/py/mod.rs index dae8080..7f2d224 100644 --- a/ica-rs/src/py/mod.rs +++ b/ica-rs/src/py/mod.rs @@ -451,6 +451,7 @@ fn init_py_with_env_path(path: &str) { // 使用 Py_InitializeFromConfig 初始化 python let status = pyo3::ffi::Py_InitializeFromConfig(&config as *const _); + pyo3::ffi::PyEval_SaveThread(); // 清理配置 pyo3::ffi::PyConfig_Clear(config_ptr); match status._type { @@ -513,11 +514,19 @@ pub async fn post_py() -> anyhow::Result<()> { status.config.sync_status_to_config(); status.config.write_to_default()?; - // stop_tasks().await; + stop_tasks().await; + unsafe { + if !pyo3::ffi::Py_FinalizeEx() == 0 { + event!(Level::ERROR, "Python 退出失败 (不过应该无所谓)"); + } + } Ok(()) } async fn stop_tasks() { + if call::PY_TASKS.lock().await.is_empty() { + return ; + } let waiter = tokio::spawn(async { call::PY_TASKS.lock().await.join_all().await; }); @@ -526,7 +535,7 @@ async fn stop_tasks() { event!(Level::INFO, "Python 任务完成"); } _ = tokio::signal::ctrl_c() => { - // call::PY_TASKS.lock().await.cancel_all(); + call::PY_TASKS.lock().await.cancel_all(); event!(Level::INFO, "Python 任务被中断"); } }