Tokio Fusion

4 months ago 17

A high-performance thread pool service built on top of Tokio, providing an easy-to-use API for asynchronous task execution.

  • 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
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() { // 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.unwrap(); // Wait for the result let result = handle.await_result().await; println!("Task result: {:?}", result); }
use std::sync::Arc; use tokio_fusion::ThreadPoolBuilder; #[tokio::main] async fn main() { // Create a thread pool with custom configuration let thread_pool = Arc::new( ThreadPoolBuilder::new() .worker_threads(8) .queue_capacity(1000) .build() ); // Use the thread pool... }
use std::sync::Arc; use tokio_fusion::{ThreadPool, BatchExecutor}; #[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); } // Execute all tasks and collect results let results = batch.execute().await; // Or stream results as they complete let mut stream = batch.execute_stream().await; while let Some(result) = stream.next().await { println!("Got result: {:?}", result); } }

Add this to your Cargo.toml:

[dependencies] tokio-fusion = "0.1"

This project is licensed under the Apache-2.0 License. See the LICENSE file for details.

Read Entire Article