pub struct Executor { /* private fields */ }
Expand description
A collection of threads for executing tasks and blocking jobs.
The methods of this struct are defined on &Arc<Executor>
.
Implementations
sourceimpl Executor
impl Executor
sourcepub fn default() -> Arc<Executor>
pub fn default() -> Arc<Executor>
Creates a new executor with 4 async threads and 4 blocking threads.
Panics
Panics when it fails to start the threads.
sourcepub fn new(
num_async_threads: usize,
num_blocking_threads: usize
) -> Result<Arc<Executor>, NewThreadPoolError>
pub fn new(
num_async_threads: usize,
num_blocking_threads: usize
) -> Result<Arc<Executor>, NewThreadPoolError>
Creates a new executor.
num_async_threads
is the number of threads to use for executing async
tasks.
num_blocking_threads
is the number of threads to use for executing
blocking jobs like connecting TCP sockets and reading files.
You probably want to use default
instead of this.
Errors
Returns an error when a parameter is invalid or it fails to start threads.
sourcepub fn with_name(
async_threads_name: &'static str,
num_async_threads: usize,
blocking_threads_name: &'static str,
num_blocking_threads: usize
) -> Result<Arc<Executor>, NewThreadPoolError>
pub fn with_name(
async_threads_name: &'static str,
num_async_threads: usize,
blocking_threads_name: &'static str,
num_blocking_threads: usize
) -> Result<Arc<Executor>, NewThreadPoolError>
Creates a new executor with thread names prefixed with name
.
For example, with_name("api_async", 4, "api_blocking", 100)
creates 4 threads named "api_async0"
through "api_async3"
and 100 threads named "api_blocking0"
through "api_blocking99"
.
Errors
Returns an error when a parameter is invalid or it fails to start threads.
sourcepub fn schedule_blocking<T>(
self: &Arc<Executor>,
f: impl FnOnce() -> T + Send + 'static
) -> Promise<T>ⓘNotable traits for Promise<T>impl<T> Future for Promise<T> where
T: 'static + Send, type Output = T;
where
T: 'static + Send,
pub fn schedule_blocking<T>(
self: &Arc<Executor>,
f: impl FnOnce() -> T + Send + 'static
) -> Promise<T>ⓘNotable traits for Promise<T>impl<T> Future for Promise<T> where
T: 'static + Send, type Output = T;
where
T: 'static + Send,
T: 'static + Send, type Output = T;
Schedules f
to run on any available thread in the blocking thread pool.
Returns immediately.
Await the returned promise to get the result of f
.
If f
panics, the promise will never complete.
Puts f
in a
Box
before adding it to the thread pool queue.
sourcepub fn spawn(
self: &Arc<Executor>,
fut: impl Future<Output = ()> + Send + 'static
)
pub fn spawn(
self: &Arc<Executor>,
fut: impl Future<Output = ()> + Send + 'static
)
Adds a task that will execute fut
.
The task runs on any available worker thread.
The task runs until fut
completes or the Executor is dropped.
Returns immediately.
Uses
std::boxed::Box::pin
to make the future
Unpin
.
You can use spawn_unpin
to avoid this allocation.
Example:
let executor = safina_executor::Executor::default();
executor.spawn(async move {
an_async_fn().await.unwrap();
});
sourcepub fn spawn_unpin(
self: &Arc<Executor>,
fut: impl Future<Output = ()> + Send + Unpin + 'static
)
pub fn spawn_unpin(
self: &Arc<Executor>,
fut: impl Future<Output = ()> + Send + Unpin + 'static
)
Adds a task that will execute fut
.
The task runs on any available worker thread.
The task runs until fut
completes or the Executor is dropped.
Returns immediately.
Note that fut
must be
Unpin
.
You can use
std::boxed::Box::pin
to make it Unpin. The spawn
function does this for you.
Or use pin_utils::pin_mut
to do it with unsafe code that does not allocate memory.
sourcepub fn block_on<R>(
self: &Arc<Executor>,
fut: impl Future<Output = R> + 'static
) -> R
pub fn block_on<R>(
self: &Arc<Executor>,
fut: impl Future<Output = R> + 'static
) -> R
Executes the future on the current thread and returns its result.
fut
can call spawn
to create tasks.
Those tasks run on the executor and will continue even after
fut
completes and this call returns.
Uses
std::boxed::Box::pin
to make the future
Unpin
.
You can use block_on_unpin
to avoid this allocation.
Panics
Panics if the future panics.
Example
let executor = safina_executor::Executor::default();
let result = executor.block_on(async {
prepare_request().await?;
execute_request().await
})?;
sourcepub fn block_on_unpin<R>(
self: &Arc<Executor>,
fut: impl Future<Output = R> + Unpin + 'static
) -> R
pub fn block_on_unpin<R>(
self: &Arc<Executor>,
fut: impl Future<Output = R> + Unpin + 'static
) -> R
Executes the future on the current thread and returns its result.
fut
can call spawn
to create tasks.
Those tasks run on the executor and will continue even after
fut
completes and this call returns.
Note that fut
must be
Unpin
.
You can use
std::boxed::Box::pin
to make it Unpin. The block_on
function does this for you.
Or use pin_utils::pin_mut
to do it with unsafe code that does not allocate memory.
Panics
Panics if the future panics.
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for Executor
impl Send for Executor
impl Sync for Executor
impl Unpin for Executor
impl UnwindSafe for Executor
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more