Expand description
Compute pool implementation with tokio-rayon integration
The ComputePool allows multiple async tasks to concurrently submit different
types of parallel work to a shared Rayon thread pool. This enables efficient
CPU utilization without manual thread management.
§Concurrent Usage Example
ⓘ
use std::sync::Arc;
use dynamo_runtime::compute::ComputePool;
use rayon::prelude::*;
async fn concurrent_processing(pool: Arc<ComputePool>) {
// Task 1: Using scope for dynamic task generation
let task1 = tokio::spawn({
let pool = pool.clone();
async move {
pool.execute_scoped(|scope| {
// Dynamically spawn tasks based on runtime conditions
for i in 0..100 {
scope.spawn(move |_| {
// CPU-intensive work
let mut sum = 0u64;
for j in 0..1000 {
sum += (i * j) as u64;
}
sum
});
}
}).await
}
});
// Task 2: Using parallel iterators for batch processing
let task2 = tokio::spawn({
let pool = pool.clone();
async move {
let data: Vec<u32> = (0..10000).collect();
pool.install(|| {
data.par_chunks(100)
.map(|chunk| chunk.iter().sum::<u32>())
.collect::<Vec<_>>()
}).await
}
});
// Both tasks run concurrently, sharing the same thread pool
let (result1, result2) = tokio::join!(task1, task2);
}§Thread Pool Sharing
The Rayon thread pool uses work-stealing to efficiently distribute work from multiple concurrent sources:
- Tasks from
scope.spawn()are pushed to thread-local deques - Parallel iterators distribute work across all threads
- Idle threads steal work from busy threads
- No coordination needed between different parallelization patterns
Structs§
- Compute
Handle - A handle to a compute task that’s currently running
- Compute
Pool - A compute pool that manages CPU-intensive operations
Traits§
- Compute
Pool Ext - Extension trait for ComputePool with additional patterns