WorkerPool

Struct WorkerPool 

Source
pub struct WorkerPool<D, S>
where S: WorkerTemplate,
{ /* private fields */ }
Expand description

WorkerPool manages a pool of worker threads for parallel task execution.

It allows spawning workers, sending data to them, and managing their lifecycle.

Implementations§

Source§

impl<D: Send + 'static, S: WorkerTemplate> WorkerPool<D, S>

Source

pub fn new(len: u64, template: S, draw_hz: u8) -> Self

Creates a new WorkerPool with a specified number of workers, a worker template, and a draw frequency.

§Arguments
  • len - The number of workers in the pool.
  • template - The template for creating progress bars for each worker.
  • draw_hz - The frequency at which the progress bars are drawn (frames per second).
Source

pub fn new_task_id(&self) -> Uid

Generates a new unique task ID.

Source

pub fn spawn_worker<F, Fut>(&mut self, f: F) -> Uid
where Fut: Future<Output = Result<()>> + Send + 'static, Fut::Output: Send + 'static, F: Fn(D, MainProgress<S>) -> Fut + Send + 'static,

Spawns a new worker in the pool.

§Arguments
  • f - A closure that represents the worker’s task. It takes data D and a MainProgress instance as input and returns a Result.
Source

pub fn main_ui(&self) -> MainProgress<S>

Returns a clone of the main progress instance.

Source

pub async fn sigint(&mut self)

Handles the SIGINT signal, setting a prefix message and stopping all workers.

Source

pub async fn send_seqcst(&mut self, data: D) -> Result<(), D>

Sends data to the first available worker in the pool.

§Arguments
  • data: The data to send to the worker.

Returns: Result<(), D>

Source

pub async fn send_to(&mut self, id: Uid, data: D) -> Result<(), D>

Sends data directly to the worker by its id.

§Arguments
  • id - The ID of the worker to send data to.
  • data - The data to send.

returns: Result<(), D>

§Panics

Panics if the pool naver been spawned a worker.

Source

pub fn thead_count(&self) -> usize

Returns the number of active threads in the pool.

Source

pub async fn join_all(self)

Waits for all workers to complete their tasks.

Source§

impl<D: Send, S: WorkerTemplate> WorkerPool<D, S>

Source

pub fn get_style(template: impl AsRef<str>) -> ProgressStyle

Retrieves a ProgressStyle based on the provided template.

This style includes customized progress characters, date formatting, and status indicators.

Source

pub fn println(&self, line: impl AsRef<str>) -> Result<(), Error>

Prints a line to the UI.

Source

pub fn eprintln(&self, line: impl AsRef<str>) -> Result<(), Error>

Prints an error line to the UI.

Source

pub fn horizontal_line(len: usize) -> String

Generates a horizontal line of a specified length.

Source

pub fn vertical_line(len: usize) -> String

Generates a vertical line of a specified length.

Auto Trait Implementations§

§

impl<D, S> Freeze for WorkerPool<D, S>
where S: Freeze,

§

impl<D, S> !RefUnwindSafe for WorkerPool<D, S>

§

impl<D, S> Send for WorkerPool<D, S>
where D: Send,

§

impl<D, S> !Sync for WorkerPool<D, S>

§

impl<D, S> Unpin for WorkerPool<D, S>
where S: Unpin,

§

impl<D, S> UnwindSafe for WorkerPool<D, S>
where S: UnwindSafe,

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.