Struct worker_pool::WorkerPool
source · [−]pub struct WorkerPool<Up, Down> where
Up: Send + 'static,
Down: Send + 'static, { /* private fields */ }Expand description
The main struct, represents a pool of worker. The owner of this struct is the “Manager”, while the threads handled by this struct are the “Workers”.
Example:
use worker_pool::WorkerPool;
let mut pool: WorkerPool<String, ()> = WorkerPool::new(100);
pool.execute(|tx, _rx| {
tx.send(String::from("Hello"));
tx.send(String::from("world!"));
});
assert_eq!(pool.stop().collect::<Vec<_>>().join(" "), "Hello world!");Implementations
sourceimpl<Up, Down> WorkerPool<Up, Down> where
Up: Send + 'static,
Down: Send + 'static,
impl<Up, Down> WorkerPool<Up, Down> where
Up: Send + 'static,
Down: Send + 'static,
pub fn new(buffer_length: usize) -> Self
pub fn execute<F>(&mut self, callback: F) where
F: FnOnce(WorkerSender<Up>, Receiver<DownMsg<Down>>),
F: Send + 'static,
pub fn execute_many<F>(&mut self, n_workers: usize, callback: F) where
F: FnOnce(WorkerSender<Up>, Receiver<DownMsg<Down>>),
F: Clone + Send + 'static,
sourcepub fn buffer_length(&self) -> usize
pub fn buffer_length(&self) -> usize
Returns the maximum length of the message queue
sourcepub fn recv(&self) -> Result<Up, RecvError>
pub fn recv(&self) -> Result<Up, RecvError>
Receives a single message from a worker; blocking.
If you need to call this function repeatedly, then consider iterating over the result of recv_burst instead.
sourcepub fn recv_burst<'b>(&'b mut self) -> RecvBurstIterator<'b, Up>ⓘNotable traits for RecvBurstIterator<'a, Up>impl<'a, Up: Send + 'static> Iterator for RecvBurstIterator<'a, Up> type Item = Up;
pub fn recv_burst<'b>(&'b mut self) -> RecvBurstIterator<'b, Up>ⓘNotable traits for RecvBurstIterator<'a, Up>impl<'a, Up: Send + 'static> Iterator for RecvBurstIterator<'a, Up> type Item = Up;
Returns an iterator that will yield a “burst” of messages. This iterator will respect causality, meaning that it will not yield any message that were sent after it was created. You can thus safely iterate over all of the elements of this iterator without risking a livelock.
sourcepub fn stop(&mut self) -> RecvAllIterator<Up>ⓘNotable traits for RecvAllIterator<Up>impl<Up: Send + 'static> Iterator for RecvAllIterator<Up> type Item = Up;
pub fn stop(&mut self) -> RecvAllIterator<Up>ⓘNotable traits for RecvAllIterator<Up>impl<Up: Send + 'static> Iterator for RecvAllIterator<Up> type Item = Up;
Stops the execution of all threads, returning an iterator that will yield and join all of the messages from the workers. As soon as this function returns, the WorkerPool will be back to its starting state, allowing you to execute more tasks immediately.
The returned iterator will read all of the remaining messages one by one. Once the last message is received, it will join all threads.
sourcepub fn stop_and_join(&mut self) -> Vec<Up>
pub fn stop_and_join(&mut self) -> Vec<Up>
Stops the execution of all threads and joins them. Returns a Vec containing all of the remaining yielded values.
Note that the returned Vec will ignore the buffer_length limitation.
sourcepub fn broadcast_one(
&mut self,
msg: DownMsg<Down>
) -> Result<(), SendError<DownMsg<Down>>>
pub fn broadcast_one(
&mut self,
msg: DownMsg<Down>
) -> Result<(), SendError<DownMsg<Down>>>
Sends msg to a single worker, in a round-robin fashion.
Returns Err if there is no worker or if the worker has dropped its receiver.
pub fn get(
&self,
index: usize
) -> Option<(&JoinHandle<()>, Sender<DownMsg<Down>>)>
Auto Trait Implementations
impl<Up, Down> !RefUnwindSafe for WorkerPool<Up, Down>
impl<Up, Down> Send for WorkerPool<Up, Down>
impl<Up, Down> !Sync for WorkerPool<Up, Down>
impl<Up, Down> Unpin for WorkerPool<Up, Down> where
Down: Unpin,
Up: Unpin,
impl<Up, Down> !UnwindSafe for WorkerPool<Up, Down>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more