pub struct Worker<T, R>{ /* private fields */ }
Expand description
A worker that processes tasks in parallel using multiple worker threads.
Implementations§
Source§impl<T, R> Worker<T, R>
impl<T, R> Worker<T, R>
Sourcepub fn with_num_threads(
num_worker_threads: usize,
worker_function: fn(T, &State) -> Option<R>,
) -> Worker<T, R>
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.
Sourcepub fn new(worker_function: fn(T, &State) -> Option<R>) -> Worker<T, R>
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.
Sourcepub fn cancel_tasks(&self)
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.
Sourcepub fn add_task(&self, task: T)
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.
Sourcepub fn add_tasks(&self, tasks: impl IntoIterator<Item = T>)
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.
Sourcepub fn get(&self) -> Option<R>
pub fn get(&self) -> Option<R>
Return the next result. If no result is available, return None. This function will not block.
Sourcepub fn get_blocking(&self) -> Option<R>
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.
Sourcepub fn get_iter(&self) -> impl Iterator<Item = R>
pub fn get_iter(&self) -> impl Iterator<Item = R>
Return an iterator over all available results. This function will not block.
Sourcepub fn get_iter_blocking(&self) -> impl Iterator<Item = R>
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.
Sourcepub fn get_vec(&self) -> Vec<R>
pub fn get_vec(&self) -> Vec<R>
Receive all available results and return them in a vector. This function will not block.
Sourcepub fn get_vec_blocking(&self) -> Vec<R>
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.
Sourcepub fn get_buffered(&self, buffer: &mut [R]) -> usize
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.
Sourcepub fn get_buffered_blocking(&self, buffer: &mut [R]) -> usize
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.
Sourcepub fn current_queue_size(&self) -> usize
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.
Sourcepub fn num_pending_tasks(&self) -> usize
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.