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 panic is resumed. A second cleanup panic, or a cleanup panic while the runtime is dropped during an existing unwind, aborts the process.
A process can create at most 65,535 runtimes, and at most 65,535 threads can create one. Neither is reused, and the process aborts when either runs out.
fn assert_send<T: Send>() {}
assert_send::<ntex_rt::Runtime>();Implementations§
Source§impl Runtime
impl Runtime
Sourcepub fn builder() -> RuntimeBuilder
pub fn builder() -> RuntimeBuilder
Creates a runtime builder.
Sourcepub fn with_current<T, F: FnOnce(&Self) -> T>(f: F) -> T
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.
Sourcepub fn spawn<F: Future + 'static>(&self, future: F) -> JoinHandle<F::Output> ⓘ
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.
Sourcepub unsafe fn spawn_unchecked<F: Future>(
&self,
future: F,
) -> JoinHandle<F::Output> ⓘ
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.
Sourcepub fn spawn_detached<F: Future + 'static>(&self, future: F)
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.
Sourcepub fn awake(&self)
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.
Sourcepub fn poll(&self) -> PollResult
pub fn poll(&self) -> PollResult
Polls the runtime and runs scheduled tasks.
Sourcepub fn now(&self) -> Instant
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.
Sourcepub fn busy_duration(&self) -> Duration
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.
Sourcepub fn current_turn(&self) -> Option<Duration>
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.
Sourcepub fn poll_count(&self) -> u64
pub fn poll_count(&self) -> u64
Returns the number of times poll ran scheduled tasks.
Sourcepub fn poll_again_count(&self) -> u64
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.
Sourcepub fn self_wake_count(&self) -> u64
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.
Sourcepub fn poll_histogram(&self) -> Histogram
pub fn poll_histogram(&self) -> Histogram
Sourcepub fn block_on<F: Future>(&self, future: F, driver: &dyn Driver) -> F::Output
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.