std/task
Concurrent work: Task, a computation that finishes later, and Channel, a queue between two tasks.
Asynchrony lives in the type: a function that returns Task<Value> may call await(), and its body produces the
Value - the same way a function that returns Result may use ?. Task and Channel are the two shared types
that connect concurrent work; everything else is copied when it crosses into one.
Every task is cancellable (Task.cancel), and a cancellation is passed on rather than answered: await() answers
the Value, and a task that awaits a task that ended cancelled is cancelled itself, right there - its scopes end and
whoever awaits it is cancelled in turn. So an IO line is file.addLine(text).await()?, one ? for the failure of
the work and nothing for a cancellation. Task.result is the one way to observe a cancellation as a value
(Cancelled), for a supervisor or for code that cancelled a task and wants to confirm it.
Modules
std/task/libConcurrent work: [Task], a computation that finishes later, and [Channel], a queue between two tasks.
Everything
- fn
bothWaits for two tasks of different types at once:const (user, posts) = both(fetchUser(1), fetchPosts(1)).await(). - type
CancelledA task that stopped because somebody asked it to, as [Task.result] answers it. - type
ChannelA stream in memory, of which one holder has both ends: a queue between two tasks. - type
ChannelClosedNobody is reading any more: the reading end of the channel was closed. - extend
ChannelClosed with From<ChannelClosed>A producer whose own failure isChannelClosedneeds no conversion to learn that nobody reads any more. - type
ChannelSinkThe writing end of a [Channel], aSink<Item, ChannelClosed>:addfinishes once a reader took the item,end()andclose()end the stream. - type
ChannelSourceThe reading end of a [Channel], aSource<Item, Never>: everynext()waits for the next item, andclose()tells the writers to stop. - fn
offloadRunsbodyon a thread of the blocking pool instead of on a worker, so a call that blocks - reading a file, running a child process, a C library that waits - does not stall the other tasks of the worker that asked. - fn
pausePuts this task at the back of the queue and lets every other ready task run first. - fn
spawnRuns the closure as a new task, in parallel. - type
TaskA computation that finishes later, running from the moment it is created (spawn { ... }, or calling a function that returns one). - type
TimedOutA deadline that passed: what [Task.within] answers when the limit came first. - type
WorkersThe worker pool of this process: how many operating-system threads run its tasks.