Skip to main content

ParallelBatchExecutor

Struct ParallelBatchExecutor 

Source
pub struct ParallelBatchExecutor { /* private fields */ }
Expand description

Fixed-width parallel batch executor backed by scoped standard threads.

The executor creates scoped worker threads for each parallel batch run and shuts them down before BatchExecutor::execute returns. Because the workers are scoped to the call, tasks may borrow data from the caller and do not need to be 'static.

Default uses Self::DEFAULT_SEQUENTIAL_THRESHOLD, so batches with at most 100 declared tasks run through SequentialBatchExecutor to avoid thread setup overhead. Configure sequential_threshold(0) through Self::builder when every non-empty batch should use parallel workers.

use qubit_batch::{
    BatchExecutor,
    ParallelBatchExecutor,
};

let executor = ParallelBatchExecutor::builder()
    .thread_count(2)
    .sequential_threshold(0)
    .build()
    .expect("parallel executor configuration should be valid");

let outcome = executor
    .for_each(0..4, |value| {
        assert!(value < 4);
        Ok::<(), &'static str>(())
    })
    .expect("range length should be exact");

assert!(outcome.is_success());

Implementations§

Source§

impl ParallelBatchExecutor

Source

pub const DEFAULT_REPORT_INTERVAL: Duration

Default interval between progress callbacks.

Source

pub const DEFAULT_SEQUENTIAL_THRESHOLD: usize = 100

Default maximum batch size that still uses sequential execution.

Source

pub fn default_thread_count() -> usize

Returns the default worker-thread count.

§Returns

The available CPU parallelism, or 1 if it cannot be detected.

Source

pub fn builder() -> ParallelBatchExecutorBuilder

Creates a builder for configuring a parallel batch executor.

§Returns

A builder initialized with default settings.

Source

pub fn new(thread_count: usize) -> Result<Self, ParallelBatchExecutorBuildError>

Creates a parallel batch executor with thread_count workers.

§Parameters
  • thread_count - Number of scoped worker threads to use.
§Returns

A configured parallel batch executor.

§Errors

Returns ParallelBatchExecutorBuildError::ZeroThreadCount when thread_count is zero.

Source

pub const fn thread_count(&self) -> usize

Returns the configured worker-thread count.

§Returns

The maximum number of scoped worker threads used for one batch.

Source

pub const fn sequential_threshold(&self) -> usize

Returns the configured sequential fallback threshold.

§Returns

The maximum task count that still runs sequentially.

Source

pub const fn report_interval(&self) -> Duration

Returns the configured progress-report interval.

§Returns

The minimum interval between due-based running progress callbacks.

Source

pub fn reporter(&self) -> &Arc<dyn ProgressReporter>

Returns the progress reporter used by this executor.

§Returns

A shared reference to the configured progress reporter.

Trait Implementations§

Source§

impl BatchExecutor for ParallelBatchExecutor

Source§

fn execute_with_count<T, E, I>( &self, tasks: I, count: usize, ) -> Result<BatchOutcome<E>, BatchExecutionError<E>>
where I: IntoIterator<Item = T>, T: Runnable<E> + Send, E: Send,

Executes the batch on scoped standard threads when the batch is large enough.

§Parameters
  • tasks - Task source for the batch.
  • count - Declared task count expected from tasks.
§Returns

A structured batch result when the declared task count matches, or a batch-count mismatch error with the attached partial result.

§Errors

Returns BatchExecutionError when tasks yields fewer or more tasks than count.

§Panics

Panics from tasks are captured in the result. Panics from the configured progress reporter are propagated to the caller.

Source§

fn execute<T, E, I>( &self, tasks: I, ) -> Result<BatchOutcome<E>, BatchExecutionError<E>>
where I: IntoIterator<Item = T>, I::IntoIter: ExactSizeIterator, T: Runnable<E> + Send, E: Send,

Executes a batch of runnable tasks whose iterator exposes an exact length. Read more
Source§

fn call<C, R, E, I>( &self, tasks: I, ) -> Result<BatchCallResult<R, E>, BatchExecutionError<E>>
where I: IntoIterator<Item = C>, I::IntoIter: ExactSizeIterator, C: Callable<R, E> + Send, R: Send, E: Send,

Executes callable tasks whose iterator exposes an exact length. Read more
Source§

fn call_with_count<C, R, E, I>( &self, tasks: I, count: usize, ) -> Result<BatchCallResult<R, E>, BatchExecutionError<E>>
where I: IntoIterator<Item = C>, C: Callable<R, E> + Send, R: Send, E: Send,

Executes callable tasks with an explicit declared count and collects success values by index. Read more
Source§

fn for_each<Item, E, I, F>( &self, items: I, action: F, ) -> Result<BatchOutcome<E>, BatchExecutionError<E>>
where I: IntoIterator<Item = Item>, I::IntoIter: ExactSizeIterator, Item: Send, F: Fn(Item) -> Result<(), E> + Send + Sync, E: Send,

Applies action to every item whose iterator exposes an exact length. Read more
Source§

fn for_each_with_count<Item, E, I, F>( &self, items: I, count: usize, action: F, ) -> Result<BatchOutcome<E>, BatchExecutionError<E>>
where I: IntoIterator<Item = Item>, Item: Send, F: Fn(Item) -> Result<(), E> + Send + Sync, E: Send,

Applies action to every item using an explicit declared count. Read more
Source§

impl Clone for ParallelBatchExecutor

Source§

fn clone(&self) -> ParallelBatchExecutor

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for ParallelBatchExecutor

Source§

fn default() -> Self

Creates a default parallel batch executor.

§Returns

A default-configured parallel batch executor.

§Panics

Panics if the default configuration fails validation.

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.