core::task
이 페이지의 내용
- Re-exports
- Structs
CancellationError1Join1TaskId1TimeoutError1- Functions
- Implementations
impl Pool1impl Timeout1
API 참조는 영어로만 있습니다. 표준 라이브러리 소스의 영어 문서 주석에서 만듭니다.
tasks on the work-stealing executor
and block_on; Duration / Instant are
core::time.
One executor per process, started on first use: N workers on threads
that live as long as the process (N = online CPUs or NABIR_WORKERS),
the blocking lane and the I/O driver. block_on and par::* attach
the calling thread as a guest until their future and its
spawn_local tasks are done. Without threads (wasm32-wasip2, the
browser) the guest runs everything itself, waiting in nabi_io_wait.
Cancellation: Task::cancel sets a flag and
wakes the task; each await checks it on entry and after each resume
and leaves the body like a return, running finally / using. A
task cancelled before its first poll is never polled.
NABIR_THREAD_PER_CORE=1 keeps every task on one worker's
local queue, so nothing is stolen. NABIR_DYNAMIC_WORKERS=1
wakes only active workers and activates one more when all are
busy.
Re-exports
core::task::parking::{readable, registrations, wake_io, writable}Source
pub use core::task::parking::{readable, registrations, wake_io, writable}readablewake_iowritable
Structs
SelectFutureSource
pub struct SelectFuture<F>The future select returns: (index, output) of the first future to
complete.
CancellationErrorSource
pub struct CancellationErrorA task was cancelled.
What went wrong, for display.
JoinSource
pub struct Join<T>Task::join.
Sourcepub fn poll(§ref self, ref ctx: Context) throws (CancellationError) -> Poll<T> One step of
join:Readywith the result once the task finished,Pendingbefore. ThrowsCancellationErrorfor a cancelled task.
Trait implementations 1
impl<T> Debug for Join<T>
TaskIdSource
pub struct TaskIdIdentifies a task for as long as the process runs (ids are never
reused): Task::id, task::id().
The id as a number, unique for the process's run.
TimeoutErrorSource
pub struct TimeoutErrortask::timeout ran out.
What went wrong, for display.
TaskSource
pub struct Task<T>A spawned task; await it for its result.
Whether the task has finished, with a result or cancelled.
This task's id (what
task::id()returns inside it).Ask the task to stop: it is woken, its current
awaitleaves the body (running thefinally/usingcleanups on the way out) and it finishes without a result; one not yet polled never runs. Awaiting it then panics; usetry await t.join()to seeCancellationError. A task that reaches no cancellation point after the cancel finishes normally, with its result.Whether
cancelwas called on the task.Await the result, throwing
CancellationErrorfor a cancelled task.
Functions
joinSource
pub async fn join<I: IntoIterator<Item = Task<T>>, T>(tasks: I) -> List<T> Every task's result, in order.
raceSource
pub async fn race<I: IntoIterator<Item = Task<T>>, T>(tasks: I) -> T The result of whichever task finishes first; ties go to the
earlier one. The others are cancelled (by the time race returns
they may still run their cleanups), and so are all of them when the
awaiting task is. Panics on no tasks.
selectSource
pub const fn select<I: IntoIterator<Item = F>, F: Future>(iter: I) -> SelectFuture<F> The first of futs to complete, with its index: let (i, v) = await task::select([a, b]). Unlike race the futures are not spawned: they run inside
the awaiting task, polled in list order (ties go to the lower index).
The futures share one type: async { .. } blocks with the same
output do (wrap differing outputs, async { Msg::A(await a) }).
Once one completes, every other one that has started is polled once
more as cancelled (its await leaves through finally / using, as
for Task::cancel) and dropped; one whose cleanups await goes on as a
task of its own until they end, and select does not wait for it.
Panics on no futures.
spawn_localSource
pub fn spawn_local<F: Future>(fut: F) -> Task<F::Output> Run fut as a new task on this worker only (it need not be Send).
From a task a block_on thread happens to poll, that is one pool
worker, and the task starts once that poll ends.
current_idSource
pub fn current_id() -> TaskId | none The running task's id, or none outside a task (sync code, a
spawn_blocking thread).
idSource
pub fn id() -> TaskId The running task's id; panics outside a task (current_id).
registrationsSource
pub fn registrations() -> usize Parked wakers (all keys) plus pending timers of unfinished tasks: a diagnostic (a cancelled task leaves none behind).
sleepSource
pub async fn sleep(dur: Duration) Completes once dur has passed since its first poll.
sleep_untilSource
pub async fn sleep_until(when: Instant) Completes at when.
timeoutSource
pub async fn timeout<T>(dur: Duration, task: Task<T>) throws (TimeoutError) -> T task's result, or TimeoutError once dur has passed since the
first poll: try await task::timeout(d, t). On expiry the
task is cancelled (it may still run its cleanups when timeout
throws), and so it is when the awaiting task is.
yield_nowSource
pub async fn yield_now() Let the other ready tasks run once.
block_onSource
pub fn block_on<F: Future>(fut: F) -> F::Output Run fut to completion from synchronous code. It returns
once the spawn_local tasks pinned to this thread are done too.
try_block_onSource
pub fn try_block_on<T, E>(fut: TryAsyncFuture<T, E>) throws (E) -> T block_on for a throwing async body: its error is thrown here, with
the site of the throw that raised it (on whichever thread).
worker_countSource
pub fn worker_count() -> usize Workers of the executor this thread runs tasks on (1 outside one, and without threads, where the one thread runs every task).
cpu_countSource
pub fn cpu_count() -> usize Online CPUs (the default pool size).
spawnSource
pub fn spawn<F: Future + Send>(fut: F) -> Task<F::Output> where F::Output: Send Run fut as a new task on any worker, concurrently with the
spawner. Needs a running executor (inside async fn main /
block_on). The task gets its own copy of every collection fut
holds, what its coroutine captured included: the spawner
keeps the originals.
spawn_blockingSource
pub fn spawn_blocking<T: Send>(f: fn() -> T + Send) -> Task<T> Run f on the executor's blocking lane as a task (tokio's
spawn_blocking). For syscalls with no readiness to wait on, like
regular files under kqueue / epoll. f is queued at once. A cancel
before the lane takes it means it never runs (join throws
CancellationError); once it runs it finishes with its result, as it
has no cancellation point.
spawn_blocking_with_argSource
pub fn spawn_blocking_with_arg<V: Send, T: Send>(arg: V, f: fn(V) -> T + Send) -> Task<T> spawn_blocking that hands arg to f on the lane. f's plain
captures stay shared with the caller, so they must be Send + Sync;
arg only needs Send: it is handed over like a channel send (the
task gets a copy of every collection it reaches, and the caller can no
longer use it).
Implementations
impl Pool
Number of pool threads.
impl Timeout
Sourcepub fn poll(§ref self, ref ctx: Context) throws (TimeoutError) -> Poll<T> One step of
timeout:Readywith the result once the task finished,Pendingbefore the deadline. ThrowsTimeoutErroronce it passed.