pub struct PersistentWorkerPool<D, C>where
D: Dataset + Clone + Send + Sync + 'static,
C: Collate<D::Item> + Clone + Send + Sync + 'static,
D::Item: Send + 'static,
C::Output: Send + 'static,{ /* private fields */ }Expand description
Persistent worker pool that keeps workers alive across epochs
Unlike the regular WorkerPool, this implementation keeps worker threads alive between epochs, reducing the overhead of thread creation and destruction. This is particularly useful for training scenarios with multiple epochs.
§Examples
use std::sync::Arc;
use torsh_data::dataloader::workers::PersistentWorkerPool;
use torsh_data::dataset::TensorDataset;
use torsh_data::collate::DefaultCollate;
let dataset = Arc::new(TensorDataset::new(vec![1, 2, 3, 4, 5]));
let collate_fn = Arc::new(DefaultCollate);
let worker_pool = PersistentWorkerPool::new(dataset, collate_fn, 4);
// Use across multiple epochs
for epoch in 0..10 {
worker_pool.reset_for_epoch().unwrap();
// Submit tasks for this epoch
}
// Shutdown when done
worker_pool.shutdown().unwrap();Implementations§
Source§impl<D, C> PersistentWorkerPool<D, C>
impl<D, C> PersistentWorkerPool<D, C>
Sourcepub fn get_result(&self) -> Result<WorkerResult<C::Output>>
pub fn get_result(&self) -> Result<WorkerResult<C::Output>>
Get a result from the persistent worker pool
This method blocks until a result is available from any worker.
§Returns
The next available worker result
Sourcepub fn get_result_timeout(
&self,
timeout: Duration,
) -> Result<WorkerResult<C::Output>>
pub fn get_result_timeout( &self, timeout: Duration, ) -> Result<WorkerResult<C::Output>>
Sourcepub fn try_get_result(&self) -> Option<WorkerResult<C::Output>>
pub fn try_get_result(&self) -> Option<WorkerResult<C::Output>>
Sourcepub fn num_workers(&self) -> usize
pub fn num_workers(&self) -> usize
Sourcepub fn is_shutdown(&self) -> bool
pub fn is_shutdown(&self) -> bool
Sourcepub fn shutdown(&self) -> Result<()>
pub fn shutdown(&self) -> Result<()>
Shutdown the persistent worker pool
This method gracefully shuts down all worker threads and should be called when the pool is no longer needed.
§Returns
Result indicating success or failure of shutdown operation
Sourcepub fn reset_for_epoch(&self) -> Result<()>
pub fn reset_for_epoch(&self) -> Result<()>
Reset the pool for a new epoch (clears any pending tasks)
This method can be called between epochs to reset the pool state. In the current implementation, this is a placeholder for future epoch-specific optimizations.
§Returns
Result indicating success or failure of reset operation
Trait Implementations§
Source§impl<D, C> Drop for PersistentWorkerPool<D, C>
impl<D, C> Drop for PersistentWorkerPool<D, C>
Auto Trait Implementations§
impl<D, C> !RefUnwindSafe for PersistentWorkerPool<D, C>
impl<D, C> !Sync for PersistentWorkerPool<D, C>
impl<D, C> !UnwindSafe for PersistentWorkerPool<D, C>
impl<D, C> Freeze for PersistentWorkerPool<D, C>
impl<D, C> Send for PersistentWorkerPool<D, C>
impl<D, C> Unpin for PersistentWorkerPool<D, C>
impl<D, C> UnsafeUnpin for PersistentWorkerPool<D, C>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.