pub struct ThreadPool {
pub pool: Runtime,
/* private fields */
}Expand description
A small wrapper around the tokio runtime supporting multithreading with max concurrency limits
use tokio_thread_pool::ThreadPool;
// Create a pool with default settings
let my_pool = ThreadPool::new(
None, // optional max task concurrency (usize),
None, // optional max number of threads defaulting to the number of CPU cores on the system (usize),
None, // an optional tokio runtime that you provide with your own custom settings (tokio::Runtime)
);
// Create a pool with a limit on task concurrency
let my_pool = ThreadPool::new(Some(10), None, None); // maximimum of ten concurrent tasks running at once
// Create a pool with a limit on spawned threads
let my_pool = ThreadPool::new(None, Some(4), None); // maximimum of four threads for task allocation
// Create a pool with your own runtime provided
let my_pool = ThreadPool::new(
None,
None,
Some(tokio::runtime::Builder::new_multi_thread().build().unwrap())
);
// Spawn async tasks
let handle = my_pool.spawn(async move || {}); // return any value
// Spawn sync tasks
let handle = my_pool.spawn_blocking(move || {}); // return any value
// Get result
let result = handle.await;Fields§
§pool: RuntimeImplementations§
Source§impl ThreadPool
impl ThreadPool
Sourcepub fn new(
max_concurrency: Option<usize>,
max_threads: Option<usize>,
pool_override: Option<Runtime>,
) -> ThreadPool
pub fn new( max_concurrency: Option<usize>, max_threads: Option<usize>, pool_override: Option<Runtime>, ) -> ThreadPool
Constructs a new ThreadPool instance
§Arguments
max_concurrency (Option<usize>): optional max task concurrency
max_threads (Option<usize>): optional max number of threads defaulting to the number of CPU cores on the system
pool_override (Option<tokio::Runtime>): an optional tokio runtime that you provide with your own custom settings
// Create a pool with default settings
let my_pool = ThreadPool::new(None, None, None);
// Create a pool with a limit on task concurrency
let my_pool = ThreadPool::new(Some(10), None, None); // maximimum of ten concurrent tasks running at once
// Create a pool with a limit on spawned threads
let my_pool = ThreadPool::new(None, Some(4), None); // maximimum of four threads for task allocation
// Create a pool with your own runtime provided
let my_pool = ThreadPool::new(
None,
None,
Some(tokio::runtime::Builder::new_multi_thread().build().unwrap())
);Sourcepub fn spawn<T: Send + 'static, F: Fn() -> T + 'static + Send>(
&mut self,
task: F,
) -> JoinHandle<T>
pub fn spawn<T: Send + 'static, F: Fn() -> T + 'static + Send>( &mut self, task: F, ) -> JoinHandle<T>
Spawns an async task and returns its Handler<T>
§Arguments
task ((Fn() -> T)): The task to execute inside of the thread pool
// Create a pool with default settings
let my_pool = ThreadPool::new(None, None, None);
let my_handle = my_pool.spawn(async move || {});
let result = my_handle.await;Sourcepub fn spawn_blocking<T: Send + 'static, F: Fn() -> T + 'static + Send>(
&mut self,
task: F,
) -> JoinHandle<T>
pub fn spawn_blocking<T: Send + 'static, F: Fn() -> T + 'static + Send>( &mut self, task: F, ) -> JoinHandle<T>
Spawns a synchronous task and returns its Handler<T>
§Arguments
task ((Fn() -> T)): The task to execute inside of the thread pool
// Create a pool with default settings
let my_pool = ThreadPool::new(None, None, None);
let my_handle = my_pool.spawn_blocking(async move || {});
let result = my_handle.await;Auto Trait Implementations§
impl !Freeze for ThreadPool
impl RefUnwindSafe for ThreadPool
impl Send for ThreadPool
impl Sync for ThreadPool
impl Unpin for ThreadPool
impl UnwindSafe for ThreadPool
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
Mutably borrows from an owned value. Read more