Skip to main content

SzRuntime

Struct SzRuntime 

Source
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

Source

pub fn new() -> Self

创建 SzRuntime,worker_threads = num_cpus::get()(对齐 swoole_cpu_num()

Source

pub fn with_worker_threads(worker_threads: usize) -> Self

创建 SzRuntime,自定义 worker_threads(对齐 worker_num 配置项)

  • worker_threads = 0 会被强制为 1
Source

pub fn worker_threads(&self) -> usize

获取 worker 线程数

Source

pub fn shutdown_token(&self) -> CancellationToken

获取关闭令牌的克隆

后台任务通过 rt.shutdown_token().cancelled().await 监听关闭信号。

Source

pub fn spawn<F>(&self, future: F) -> JoinHandle<F::Output>
where F: Future + Send + 'static, F::Output: Send + 'static,

在 runtime 上 spawn 异步任务(对齐 swoole_event::defer

返回 JoinHandle,可 await 获取结果。

Source

pub fn block_on<F>(&self, future: F) -> F::Output
where F: Future,

在 runtime 上阻塞运行 future(对齐 Swoole\Runtime::enableCoroutine 入口)

调用线程会阻塞直到 future 完成。

Source

pub fn shutdown_timeout(self, timeout: Duration) -> bool

触发优雅关闭:cancel() 通知所有后台任务,等待 timeout 后 drop runtime

  • 返回 true:runtime 已关闭
  • 注意:Drop runtime 时 tokio 会等待所有任务完成,超时由调用方控制
Source

pub fn handle(&self) -> Handle

获取内部 runtime 句柄(用于在不持有 SzRuntime 时 spawn 任务)

Trait Implementations§

Source§

impl Default for SzRuntime

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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 more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

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

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more