Skip to content

Commit

Permalink
fix: tokio spawn with outer is native thread
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat616 committed Jul 20, 2024
1 parent 47ceb83 commit a2cf8ea
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
8 changes: 6 additions & 2 deletions nyanpasu-utils/src/core/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use parking_lot::RwLock;
use tokio::sync::mpsc::Sender;
use tracing_attributes::instrument;

use crate::runtime;

use super::CommandEvent;

#[instrument]
Expand Down Expand Up @@ -60,7 +62,7 @@ pub(super) fn spawn_pipe_reader<F: Fn(String) -> CommandEvent + Send + Copy + 's
Some(encoding) => Ok(encoding.decode_with_bom_removal(&buf).0.into()),
None => String::from_utf8(buf.clone()),
};
tokio::spawn(async move {
runtime::spawn(async move {
let _ = match line {
Ok(line) => tx_.send(wrapper(line)).await,
Err(e) => tx_.send(CommandEvent::Error(e.to_string())).await,
Expand All @@ -69,7 +71,9 @@ pub(super) fn spawn_pipe_reader<F: Fn(String) -> CommandEvent + Send + Copy + 's
}
Err(e) => {
let tx_ = tx.clone();
tokio::spawn(async move { tx_.send(CommandEvent::Error(e.to_string())).await });
runtime::spawn(
async move { tx_.send(CommandEvent::Error(e.to_string())).await },
);
break;
}
}
Expand Down
9 changes: 9 additions & 0 deletions nyanpasu-utils/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,12 @@ pub fn block_on<F: Future>(task: F) -> F::Output {
let runtime = RUNTIME.get_or_init(default_runtime);
runtime.block_on(task)
}

pub fn spawn<F>(task: F)
where
F: Future + Send + 'static,
F::Output: Send + 'static,
{
let runtime = RUNTIME.get_or_init(default_runtime);
runtime.spawn(task);
}

0 comments on commit a2cf8ea

Please sign in to comment.