Skip to main content

Worker

Struct Worker 

Source
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>

Source

pub fn new(queue: &'q Queue<T>, id: usize) -> Self

Construct a new Worker.

The worker will be assigned the specified ID.

§Panics
  • Panics if id >= queue.config().num_workers().get().
  • Panics if another worker has been assigned this ID already.
Source

pub const fn id(&self) -> usize

The ID of this worker.

This is the value assigned to the worker in Worker::new().

Source

pub const fn queue(&self) -> &'q Queue<T>

The associated global queue.

Source

pub const fn enqueuer(&self) -> &Rc<Enqueuer<T>>

The associated Enqueuer.

Source

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().

Source

pub fn enqueue_one(&self, task: T)

Enqueue a single task.

The task will be added to a thread-local Vec, where it will remain until it is used in the Worker’s batch.

This is a shorthand for self.enqueuer().add(task).

Source

pub async fn next(&mut self) -> Option<T>

Retrieve a single task asynchronously.

If None is returned, the task queue has been shut down.

Source

pub fn poll(&mut self, waker: &Waker) -> Poll<Option<T>>

Poll for a single task.

If None is returned, the task queue has been shut down.

Trait Implementations§

Source§

impl<T: Task> Drop for Worker<'_, T>

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§

§

impl<'q, T> !RefUnwindSafe for Worker<'q, T>

§

impl<'q, T> !Send for Worker<'q, T>

§

impl<'q, T> !Sync for Worker<'q, T>

§

impl<'q, T> !UnwindSafe for Worker<'q, T>

§

impl<'q, T> Freeze for Worker<'q, T>
where <T as Task>::Priority: Freeze,

§

impl<'q, T> Unpin for Worker<'q, T>
where <T as Task>::Priority: Unpin, T: Unpin,

§

impl<'q, T> UnsafeUnpin for Worker<'q, T>
where <T as Task>::Priority: UnsafeUnpin,

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.