pub struct SzRuntime { /* private fields */ }Expand description
SZ-Rust 异步运行时
对齐 PHP think-swoole 的运行时模型:基于 tokio multi_thread runtime,
worker 数量默认 = CPU 核数(对齐 swoole_cpu_num())。
§设计
- 封装
tokio::runtime::Runtime,对外暴露block_on/spawn入口 - 持有
CancellationToken,所有后台任务通过shutdown_token()监听关闭信号 shutdown_timeout触发关闭流程:先cancel()通知所有任务,等待超时后 drop runtime
§用法
ⓘ
use sz_rust_core::runtime::SzRuntime;
use std::time::Duration;
let rt = SzRuntime::new();
assert!(rt.worker_threads() > 0);
// spawn 后台任务
let handle = rt.spawn(async { 42 });
assert_eq!(rt.block_on(handle).unwrap(), 42);
// 优雅关闭
assert!(rt.shutdown_timeout(Duration::from_millis(100)));Implementations§
Source§impl SzRuntime
impl SzRuntime
Sourcepub fn with_worker_threads(worker_threads: usize) -> Self
pub fn with_worker_threads(worker_threads: usize) -> Self
创建 SzRuntime,自定义 worker_threads(对齐 worker_num 配置项)
worker_threads = 0会被强制为 1
Sourcepub fn worker_threads(&self) -> usize
pub fn worker_threads(&self) -> usize
获取 worker 线程数
Sourcepub fn shutdown_token(&self) -> CancellationToken
pub fn shutdown_token(&self) -> CancellationToken
获取关闭令牌的克隆
后台任务通过 rt.shutdown_token().cancelled().await 监听关闭信号。
Sourcepub fn spawn<F>(&self, future: F) -> JoinHandle<F::Output> ⓘ
pub fn spawn<F>(&self, future: F) -> JoinHandle<F::Output> ⓘ
在 runtime 上 spawn 异步任务(对齐 swoole_event::defer)
返回 JoinHandle,可 await 获取结果。
Sourcepub fn block_on<F>(&self, future: F) -> F::Outputwhere
F: Future,
pub fn block_on<F>(&self, future: F) -> F::Outputwhere
F: Future,
在 runtime 上阻塞运行 future(对齐 Swoole\Runtime::enableCoroutine 入口)
调用线程会阻塞直到 future 完成。
Sourcepub fn shutdown_timeout(self, timeout: Duration) -> bool
pub fn shutdown_timeout(self, timeout: Duration) -> bool
触发优雅关闭:cancel() 通知所有后台任务,等待 timeout 后 drop runtime
- 返回
true:runtime 已关闭 - 注意:Drop runtime 时 tokio 会等待所有任务完成,超时由调用方控制
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for SzRuntime
impl RefUnwindSafe for SzRuntime
impl Send for SzRuntime
impl Sync for SzRuntime
impl Unpin for SzRuntime
impl UnsafeUnpin for SzRuntime
impl UnwindSafe for SzRuntime
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.