Expand description
§Tokio Fusion
A high-performance thread pool service built on top of Tokio, providing an easy-to-use API for asynchronous task execution with work-stealing capabilities.
§Features
- High Performance: Built on top of Tokio’s efficient runtime
- Flexible Task Submission: Submit individual tasks or batches
- Priority Support: Assign priorities to tasks
- Streaming Results: Stream results as they complete
- Configurable: Customize worker threads and queue capacity
- Error Handling: Comprehensive error types and handling
§Quick Start
Add this to your Cargo.toml
:
[dependencies]
tokio-fusion = "0.1"
§Examples
§Basic Usage
use std::sync::Arc;
use tokio_fusion::{ThreadPool, Task, ThreadPoolResult};
async fn my_task(id: usize) -> ThreadPoolResult<String> {
// Your async task logic here
Ok(format!("Result from task {}", id))
}
#[tokio::main]
async fn main() -> ThreadPoolResult<()> {
// Create a thread pool with default configuration
let thread_pool = Arc::new(ThreadPool::default());
// Create and submit a task
let task = Task::new(my_task(1), 1);
let handle = thread_pool.submit(task).await?;
// Wait for the result
let result = handle.await_result().await?;
println!("Task result: {}", result);
Ok(())
}
§Batch Execution
use std::sync::Arc;
use tokio_fusion::{ThreadPool, BatchExecutor, ThreadPoolResult};
use futures::stream::StreamExt;
async fn my_task(id: usize) -> ThreadPoolResult<String> {
// Your async task logic here
Ok(format!("Result from task {}", id))
}
#[tokio::main]
async fn main() {
let thread_pool = Arc::new(ThreadPool::default());
let mut batch = BatchExecutor::new(Arc::clone(&thread_pool));
// Add tasks to the batch
for i in 0..5 {
batch.add_task(my_task(i), i);
}
// Stream results as they complete
let mut stream = batch.execute_stream();
while let Some(result) = stream.next().await {
println!("Got result: {:?}", result);
}
}
Re-exports§
pub use batch::BatchExecutor;
pub use config::ThreadPoolConfig;
pub use error::ThreadPoolError;
pub use error::ThreadPoolResult;
pub use pool::ThreadPool;
pub use pool::ThreadPoolBuilder;
pub use task::Task;
pub use task::TaskHandle;