将分离恢复阻塞 I/O 移出异步线程

新增 spawn_blocking 状态读取封装

sidecar 目录创建改用阻塞任务执行
This commit is contained in:
2026-09-12 10:23:32 +08:00
parent 915006c98f
commit 2a50614c11
2 changed files with 23 additions and 9 deletions
@@ -159,6 +159,12 @@ pub fn read_separation_state(path: &Path) -> Result<SeparationState, String> {
Ok(state)
}
pub async fn read_separation_state_async(path: PathBuf) -> Result<SeparationState, String> {
tokio::task::spawn_blocking(move || read_separation_state(&path))
.await
.map_err(|error| format!("读取 separation state 任务失败:{error}"))?
}
pub fn separation_dto(state: &SeparationState) -> SeparationDTO {
app_log!(
"ui_separation.dto bound_nodes={} problematic_nodes={} remaining_trees={}",
@@ -9,7 +9,7 @@ pub use patch::apply_batch_patch;
use self::batch::next_image_batch_with_size;
use super::model::*;
use super::persistence::{
project_relative_path, read_separation_state, separation_dto, separation_sidecar_dir,
project_relative_path, read_separation_state_async, separation_dto, separation_sidecar_dir,
separation_state_path, write_separation_state,
};
use super::prompt::gen_extract_prompt;
@@ -42,7 +42,13 @@ pub(crate) async fn separate_ui_impl(
);
error
})?;
fs::create_dir_all(&sidecar).map_err(|error| {
tokio::task::spawn_blocking({
let sidecar = sidecar.clone();
move || fs::create_dir_all(sidecar)
})
.await
.map_err(|error| format!("创建 separation sidecar 任务失败:{error}"))?
.map_err(|error| {
app_log!(
"ui_separation.error stage=sidecar_create asset_id={} error={error}",
asset_id
@@ -53,13 +59,15 @@ pub(crate) async fn separate_ui_impl(
let restored = state_path.exists();
let mut separation = if restored {
app_log!("ui_separation.state_restore.start asset_id={asset_id}");
read_separation_state(&state_path).map_err(|error| {
app_log!(
"ui_separation.error stage=state_restore asset_id={} error={error}",
asset_id
);
error
})?
read_separation_state_async(state_path.clone())
.await
.map_err(|error| {
app_log!(
"ui_separation.error stage=state_restore asset_id={} error={error}",
asset_id
);
error
})?
} else {
app_log!("ui_separation.state_construct.start asset_id={asset_id}");
super::tree::construct_separation_state(&state)