Struct Worker

Source
pub struct Worker<T, R>
where T: Send + 'static, R: Send + 'static,
{ /* private fields */ }
Expand description

A worker that processes tasks in parallel using multiple worker threads.

Implementations§

Source§

impl<T, R> Worker<T, R>
where T: Send + 'static, R: Send + 'static,

Source

pub fn with_num_threads( num_worker_threads: usize, worker_function: fn(T, &State) -> Option<R>, ) -> Worker<T, R>

Create a new worker with a given number of worker threads and a worker function. Spawns worker threads that will process tasks from the queue using the worker function.

Source

pub fn new(worker_function: fn(T, &State) -> Option<R>) -> Worker<T, R>

Create a new worker with a given worker function. The number of worker threads will be set to the number of available logical cores minus one. If you want to use a custom thread count, use the with_num_threads method to create a worker. Spawns worker threads that will process tasks from the queue using the worker function.

Source

pub fn cancel_tasks(&self)

Clear the task queue and cancel all tasks as soon as possible. The results of canceled tasks will be discarded. Canceling the execution of tasks requires the worker function to use the check_if_cancelled! macro.

Source

pub fn add_task(&self, task: T)

Add a task to the end of the queue. The task will be processed by one of the worker threads.

Source

pub fn add_tasks(&self, tasks: impl IntoIterator<Item = T>)

Add multiple tasks to the end of the queue. The tasks will be processed by the worker threads.

Source

pub fn get(&self) -> Option<R>

Return the next result. If no result is available, return None. This function will not block.

Source

pub fn get_blocking(&self) -> Option<R>

Return the next result. If no result is available block until a result is available. If no tasks are pending, return None.

Source

pub fn get_iter(&self) -> impl Iterator<Item = R>

Return an iterator over all available results. This function will not block.

Source

pub fn get_iter_blocking(&self) -> impl Iterator<Item = R>

Returns an iterator over all results. This function will block until all tasks have been processed.

Source

pub fn get_vec(&self) -> Vec<R>

Receive all available results and return them in a vector. This function will not block.

Source

pub fn get_vec_blocking(&self) -> Vec<R>

Block until all tasks have been processed and return all results in a vector. This function will block until all tasks have been processed.

Source

pub fn get_buffered(&self, buffer: &mut [R]) -> usize

Write available results into the buffer and return the number of results written. If the buffer is too small to hold all available results, the remaining results will be left in the queue. This function will not block.

Source

pub fn get_buffered_blocking(&self, buffer: &mut [R]) -> usize

Write all results into the buffer and return the number of results written. If the buffer is too small to hold all results, the remaining results will be left in the queue. This function will block until all tasks have been processed or the buffer is full.

Source

pub fn current_queue_size(&self) -> usize

Return the number of tasks currently in the queue. This does not include tasks that are currently being processed by worker threads.

Source

pub fn num_pending_tasks(&self) -> usize

Return the number of pending tasks. This includes tasks that are currently being processed by worker threads and tasks that are in the queue.

Trait Implementations§

Source§

impl<T, R> Drop for Worker<T, R>
where T: Send + 'static, R: Send + 'static,

Source§

fn drop(&mut self)

Drop the worker and terminate all worker threads.

Auto Trait Implementations§

§

impl<T, R> !Freeze for Worker<T, R>

§

impl<T, R> !RefUnwindSafe for Worker<T, R>

§

impl<T, R> Send for Worker<T, R>

§

impl<T, R> !Sync for Worker<T, R>

§

impl<T, R> Unpin for Worker<T, R>

§

impl<T, R> UnwindSafe for Worker<T, R>

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.