diff options
Diffstat (limited to 'monoio/src/blocking.rs')
| -rw-r--r-- | monoio/src/blocking.rs | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/monoio/src/blocking.rs b/monoio/src/blocking.rs index 7d3469d..c8262c4 100644 --- a/monoio/src/blocking.rs +++ b/monoio/src/blocking.rs @@ -1,4 +1,5 @@ //! Blocking tasks related. +//! 与阻塞任务相关的模块。 use std::{future::Future, task::Poll}; @@ -11,21 +12,24 @@ use crate::{ /// Users may implement a ThreadPool and attach it to runtime. /// We also provide an implementation based on threadpool crate, you can use DefaultThreadPool. +/// 用户可以实现一个ThreadPool并将其附加到运行时 或者 我们也提供了默认实现 DefaultThreadPool。 +/// ThreadPool trait 定义了一个 schedule_task 方法,用于在 spawn_blocking 时调用。 pub trait ThreadPool { - /// Monoio runtime will call `schedule_task` on `spawn_blocking`. - /// ThreadPool impl must execute it now or later. fn schedule_task(&self, task: BlockingTask); } /// Error on waiting blocking task. +/// 等待阻塞任务时的错误。 #[derive(Debug, Clone, Copy)] pub enum JoinError { /// Task is canceled. + /// 任务被取消。 Canceled, } /// BlockingTask is contrusted by monoio, ThreadPool impl /// will execute it with `.run()`. +/// BlockingTask 由 monoio 构造,ThreadPool impl 将使用 `.run()` 执行它。 pub struct BlockingTask { task: Option<crate::task::Task<NoopScheduler>>, blocking_vtable: &'static BlockingTaskVtable, @@ -57,7 +61,7 @@ impl Drop for BlockingTask { } impl BlockingTask { - /// Run task. + /// 运行任务。 #[inline] pub fn run(mut self) { let task = self.task.take().unwrap(); @@ -76,11 +80,14 @@ impl BlockingTask { /// BlockingStrategy can be set if there is no ThreadPool attached. /// It controls how to handle `spawn_blocking` without thread pool. +/// BlockingStrategy 可以在没有 ThreadPool 附加时设置。它控制如何处理没有线程池的 `spawn_blocking`。 #[derive(Clone, Copy, Debug)] pub enum BlockingStrategy { /// Panic when `spawn_blocking`. + /// `spawn_blocking` 时 panic。 Panic, /// Execute with current thread when `spawn_blocking`. + /// `spawn_blocking` 时使用当前线程执行。 ExecuteLocal, } @@ -89,6 +96,10 @@ pub enum BlockingStrategy { /// Users can also set `BlockingStrategy` for a runtime when there is no thread pool. /// WARNING: DO NOT USE THIS FOR ASYNC TASK! Async tasks will not be executed but only built the /// future! +/// `spawn_blocking` 用于执行具有重计算或阻塞 io 的任务(没有 +/// async)。要使用它,用户可以初始化线程池并将其附加到创建的运行时。 +/// 当没有线程池时,用户还可以为运行时设置 `BlockingStrategy`。 +/// 警告:不要将其用于异步任务!异步任务将不会执行,而只会构建 future! pub fn spawn_blocking<F, R>(func: F) -> JoinHandle<Result<R, JoinError>> where F: FnOnce() -> R + Send + 'static, @@ -110,6 +121,12 @@ where // 2. set runtime blocking strategy to `BlockingStrategy::ExecuteLocal` // Note: solution 2 will execute blocking task on current thread and may block other // tasks This may cause other tasks high latency. + + // 对于用户:如果看到此 panic,则有两个选择: + // 1. 附加共享线程池以执行阻塞任务 + // 2. 将运行时阻塞策略设置为 `BlockingStrategy::ExecuteLocal` + // 注意:解决方案 2 将在当前线程上执行阻塞任务,可能会阻塞其他任务, + // 这可能会导致其他任务高延迟。 panic!("execute blocking task without thread pool attached") } } @@ -121,13 +138,15 @@ where /// DefaultThreadPool is a simple wrapped `threadpool::ThreadPool` that implement /// `monoio::blocking::ThreadPool`. You may use this implementation, or you can use your own thread /// pool implementation. +/// DefaultThreadPool 是一个简单的包装了 `threadpool::ThreadPool` 的实现,实现了 +/// `monoio::blocking::ThreadPool`。 您可以使用此实现,也可以使用自己的线程池实现。 #[derive(Clone)] pub struct DefaultThreadPool { pool: ThreadPoolImpl, } impl DefaultThreadPool { - /// Create a new DefaultThreadPool. + /// 创建一个新的 DefaultThreadPool。 pub fn new(num_threads: usize) -> Self { let pool = ThreadPoolBuilder::default() .num_threads(num_threads) @@ -168,7 +187,6 @@ impl From<BlockingStrategy> for BlockingHandle { struct BlockingFuture<F>(Option<F>); impl<T> Unpin for BlockingFuture<T> {} - impl<F, R> Future for BlockingFuture<F> where F: FnOnce() -> R + Send + 'static, |
