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
impl ParallelBatchExecutor
Sourcepub const DEFAULT_REPORT_INTERVAL: Duration
pub const DEFAULT_REPORT_INTERVAL: Duration
Default interval between progress callbacks.
Sourcepub const DEFAULT_SEQUENTIAL_THRESHOLD: usize = 100
pub const DEFAULT_SEQUENTIAL_THRESHOLD: usize = 100
Default maximum batch size that still uses sequential execution.
Sourcepub fn default_thread_count() -> usize
pub fn default_thread_count() -> usize
Returns the default worker-thread count.
§Returns
The available CPU parallelism, or 1 if it cannot be detected.
Sourcepub fn builder() -> ParallelBatchExecutorBuilder
pub fn builder() -> ParallelBatchExecutorBuilder
Creates a builder for configuring a parallel batch executor.
§Returns
A builder initialized with default settings.
Sourcepub fn new(thread_count: usize) -> Result<Self, ParallelBatchExecutorBuildError>
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.
Sourcepub const fn thread_count(&self) -> usize
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.
Sourcepub const fn sequential_threshold(&self) -> usize
pub const fn sequential_threshold(&self) -> usize
Returns the configured sequential fallback threshold.
§Returns
The maximum task count that still runs sequentially.
Sourcepub const fn report_interval(&self) -> Duration
pub const fn report_interval(&self) -> Duration
Returns the configured progress-report interval.
§Returns
The minimum interval between due-based running progress callbacks.
Sourcepub fn reporter(&self) -> &Arc<dyn ProgressReporter>
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
impl BatchExecutor for ParallelBatchExecutor
Source§fn execute_with_count<T, E, I>(
&self,
tasks: I,
count: usize,
) -> Result<BatchOutcome<E>, BatchExecutionError<E>>
fn execute_with_count<T, E, I>( &self, tasks: I, count: usize, ) -> Result<BatchOutcome<E>, BatchExecutionError<E>>
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 fromtasks.
§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>>
fn execute<T, E, I>( &self, tasks: I, ) -> Result<BatchOutcome<E>, BatchExecutionError<E>>
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,
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,
Source§fn call_with_count<C, R, E, I>(
&self,
tasks: I,
count: usize,
) -> Result<BatchCallResult<R, E>, BatchExecutionError<E>>
fn call_with_count<C, R, E, I>( &self, tasks: I, count: usize, ) -> Result<BatchCallResult<R, E>, BatchExecutionError<E>>
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,
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,
action to every item whose iterator exposes an exact length. Read moreSource§fn for_each_with_count<Item, E, I, F>(
&self,
items: I,
count: usize,
action: F,
) -> Result<BatchOutcome<E>, BatchExecutionError<E>>
fn for_each_with_count<Item, E, I, F>( &self, items: I, count: usize, action: F, ) -> Result<BatchOutcome<E>, BatchExecutionError<E>>
action to every item using an explicit declared count. Read moreSource§impl Clone for ParallelBatchExecutor
impl Clone for ParallelBatchExecutor
Source§fn clone(&self) -> ParallelBatchExecutor
fn clone(&self) -> ParallelBatchExecutor
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more