Skip to main content

ThreadPool

Struct ThreadPool 

Source
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: Runtime

Implementations§

Source§

impl ThreadPool

Source

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())
);
Source

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;
Source

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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.