pub struct Worker<'q, T: Task> { /* private fields */ }Expand description
A thread-local view of the task queue.
This is the primary interface to takeaway. It manages a set of pending
tasks, communicating with the global Queue to distribute tasks across
the system, and provides methods for adding and retrieving tasks.
§Usage
You can obtain a Worker from Worker::new(). To enqueue tasks,
use Worker::enqueue(). To retrieve tasks, use Worker::next(). In
either case, lower-level interfaces are provided for more control.
// Construct the 'Worker'.
let mut worker = Worker::new(queue, id);
// Enqueue some initial tasks.
worker.enqueue_one(todo!());
// Process tasks.
while let Some(task) = worker.next().await {
// Execute the task.
//...
// Enqueue sub-tasks as necessary.
worker.enqueue_one(todo!());
}
// The task queue has shut down.Implementations§
Source§impl<'q, T: Task> Worker<'q, T>
impl<'q, T: Task> Worker<'q, T>
Sourcepub const fn id(&self) -> usize
pub const fn id(&self) -> usize
The ID of this worker.
This is the value assigned to the worker in Worker::new().
Sourcepub fn enqueue(&self, tasks: impl IntoIterator<Item = T>)
pub fn enqueue(&self, tasks: impl IntoIterator<Item = T>)
Enqueue a set of tasks.
The tasks will be added to a thread-local Vec, where they will
remain until they are used in the Worker’s batch.
This is a shorthand for self.enqueuer().extend(tasks). If the worker
is borrowed mutably, the Enqueuer can be used directly by cloning
the Rc returned by Worker::enqueuer().