Skip to main content

Runtime

Struct Runtime 

Source
pub struct Runtime { /* private fields */ }
Expand description

The async runtime for ntex.

This is a thread-local runtime and cannot be sent to other threads, use Handle to spawn tasks from other threads.

Dropping the runtime cancels its tasks. If cleanup panics, shutdown finishes before the first panic is resumed. Secondary cleanup panics are logged and suppressed, including when the runtime is dropped during an existing unwind.

ⓘ
fn assert_send<T: Send>() {}
assert_send::<ntex_rt::Runtime>();

Implementations§

Source§

impl Runtime

Source

pub fn new(handle: Box<dyn Notify>) -> Self

Creates a runtime with default configuration.

Source

pub fn builder() -> RuntimeBuilder

Creates a runtime builder.

Source

pub fn with_current<T, F: FnOnce(&Self) -> T>(f: F) -> T

Runs a closure with the runtime active on the current thread.

§Panics

Panics if no runtime is active on the current thread.

Source

pub fn handle(&self) -> Handle

Returns a handle to this runtime.

Source

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

Spawns a new asynchronous task, returning a JoinHandle for it.

Spawning a task enables the task to execute concurrently to other tasks. There is no guarantee that a spawned task will execute to completion.

Source

pub unsafe fn spawn_unchecked<F: Future>( &self, future: F, ) -> JoinHandle<F::Output> ⓘ

Spawns a new asynchronous task, returning a JoinHandle for it.

§Safety

The caller should ensure the captured lifetime is long enough.

Source

pub fn spawn_detached<F: Future + 'static>(&self, future: F)

Spawns a new asynchronous task without a JoinHandle.

The output is dropped when the task completes.

Source

pub fn awake(&self)

Marks the runtime as awake.

Drivers call this after returning from a blocking wait, before handling events that wake tasks. Tasks scheduled from the runtime thread do not notify the driver until poll finds no more tasks to run, the driver must call poll before blocking again.

Starts a busy period, see busy_duration.

Source

pub fn poll(&self) -> PollResult

Polls the runtime and runs scheduled tasks.

Source

pub fn now(&self) -> Instant

Returns the start of the current runtime turn.

While the runtime is busy, returns the time the driver woke up or the previous poll finished, tasks polled in the same turn observe the same instant. Returns Instant::now() while the runtime is idle.

Source

pub fn busy_duration(&self) -> Duration

Returns the total time the runtime has been busy.

The runtime is busy from awake until poll finds no more tasks to run. This includes running tasks and handling driver events, and excludes time the driver is blocked waiting for events. The value is wall-clock time, not CPU time, and includes the current busy period, so it can be read from other threads while a task runs.

Utilization over an interval is the busy duration delta divided by the elapsed time of the interval.

Source

pub fn current_turn(&self) -> Option<Duration>

Returns how long the current runtime turn has been running, None while the runtime is idle.

A turn ends when poll returns, a long turn means a task or a driver event handler keeps the runtime from making progress.

Source

pub fn poll_count(&self) -> u64

Returns the number of times poll ran scheduled tasks.

Source

pub fn poll_again_count(&self) -> u64

Returns the number of times poll returned PollResult::PollAgain.

poll runs a limited number of tasks, and returns PollAgain if scheduled tasks remain. The poll_again_count delta divided by the poll_count delta over an interval is the share of polls that left a backlog; a value close to 1 means tasks are scheduled faster than the runtime runs them.

Source

pub fn self_wake_count(&self) -> u64

Returns the number of times a task woke itself while it was polled.

A task that wakes itself, for example with wake_by_ref() or a yield, is scheduled again without waiting for an event. A steadily growing count points to tasks that poll in a loop, see RuntimeBuilder::self_wake_threshold.

Source

pub fn poll_histogram(&self) -> Histogram

Returns the histogram of poll durations.

A poll lasts from the start of the runtime turn, see now, until it finishes running tasks. Tasks woken during a poll wait at least until it finishes, long polls mean high scheduling latency.

Source

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

Runs the provided future.

Blocks the current thread until the future completes.

§Panics

Panics if the driver fails to run the provided future, or if a runtime is already active on the current thread. Nested calls would block the running task and its runtime until the inner future completes.

Trait Implementations§

Source§

impl Debug for Runtime

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for Runtime

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. 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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.