pub struct Executor { /* private fields */ }Expand description
A thread pool-based executor for running asynchronous tasks.
The Executor manages a pool of worker threads that execute submitted tasks.
It provides both synchronous and asynchronous draining capabilities, allowing
you to wait for all tasks to complete before shutdown.
§Platform Support
This executor automatically adapts to the target platform:
- On standard platforms, it uses OS threads via
crossbeam-channel - On WASM targets, it uses web workers for parallelism
§Examples
use some_global_executor::Executor;
// Create an executor named "worker-pool" with 4 threads
let executor = Executor::new("worker-pool".to_string(), 4);
// Get the executor's name
assert_eq!(executor.name(), "worker-pool");
// Clean up when done
executor.drain();Implementations§
Source§impl Executor
impl Executor
Sourcepub fn new(name: String, threads: usize) -> Self
pub fn new(name: String, threads: usize) -> Self
Creates a new executor with the specified name and thread count.
§Arguments
name- A descriptive name for the executor, used for logging and debuggingthreads- The number of worker threads to create in the thread pool
§Examples
use some_global_executor::Executor;
// Create an executor with 8 worker threads
let executor = Executor::new("high-performance".to_string(), 8);
// Clean up
executor.drain();§Logging
This method logs the creation of the executor using the logwise framework, including the name and thread count for debugging purposes.
Sourcepub fn name(&self) -> &str
pub fn name(&self) -> &str
Returns the name of this executor.
§Examples
use some_global_executor::Executor;
let executor = Executor::new("my-executor".to_string(), 2);
assert_eq!(executor.name(), "my-executor");
executor.drain();Sourcepub fn resize(&mut self, threads: usize)
pub fn resize(&mut self, threads: usize)
Resizes the thread pool to the specified number of threads.
This method adjusts the number of worker threads in the executor’s thread pool. The exact behavior depends on the underlying platform implementation.
§Arguments
threads- The new number of worker threads
§Examples
use some_global_executor::Executor;
let mut executor = Executor::new("dynamic".to_string(), 2);
// Increase thread count for heavy workload
executor.resize(8);
// Later, reduce thread count
executor.resize(4);
executor.drain();Source§impl Executor
impl Executor
Sourcepub fn new_default() -> Self
pub fn new_default() -> Self
Creates a new executor with default settings.
This creates an executor named “default” with a platform-appropriate number of threads (determined by the underlying system implementation).
§Examples
use some_global_executor::Executor;
let executor = Executor::new_default();
assert_eq!(executor.name(), "default");
executor.drain();Sourcepub fn drain(self)
pub fn drain(self)
Synchronously waits for all tasks in the executor to complete.
This method blocks the current thread until all spawned tasks have finished execution. It uses a busy-wait loop with thread yielding to check for task completion.
§Performance
This method logs performance warnings if the drain operation takes an unexpectedly long time, which may indicate stuck or long-running tasks.
§Examples
use some_global_executor::Executor;
use some_executor::SomeExecutor;
use some_executor::task::{Task, Configuration};
let mut executor = Executor::new("sync-drain".to_string(), 2);
// Spawn a task
let task = Task::without_notifications(
"quick-task".to_string(),
Configuration::default(),
async { 1 + 1 }
);
executor.spawn(task);
// Block until all tasks complete
executor.drain();Sourcepub fn drain_async(self) -> ExecutorDrain ⓘ
pub fn drain_async(self) -> ExecutorDrain ⓘ
Returns a future that completes when all tasks have finished.
Unlike drain(), this method returns immediately with
an ExecutorDrain future that can be awaited. This allows for
non-blocking waiting in async contexts.
§Examples
use some_global_executor::Executor;
use some_executor::SomeExecutor;
use some_executor::task::{Task, Configuration};
let mut executor = Executor::new("async-example".to_string(), 4);
// Spawn multiple tasks
for i in 0..5 {
let task = Task::without_notifications(
format!("task-{}", i),
Configuration::default(),
async move { i * 2 }
);
executor.spawn(task);
}
// Get the drain future (would be awaited in async context)
let drain_future = executor.drain_async();
// In async context: drain_future.await;
// For this example, convert back to executor and drain synchronously
let executor: Executor = drain_future.into();
executor.drain();Sourcepub fn set_as_global_executor(&self)
pub fn set_as_global_executor(&self)
Sets this executor as the global executor.
After calling this method, this executor will be used as the default for global task spawning operations throughout the application. This is useful for libraries and applications that need a shared executor instance.
§Thread Safety
The executor is cloned when set as global, so the original instance remains independent and can still be used directly.
§Examples
use some_global_executor::Executor;
let executor = Executor::new("global".to_string(), 4);
executor.set_as_global_executor();
// The global executor is now available for use by any code
// that calls some_executor::global_executor::spawn()
Sourcepub fn set_as_thread_executor(&self)
pub fn set_as_thread_executor(&self)
Sets this executor as the thread-local executor.
After calling this method, this executor will be used as the default for thread-local task spawning operations on the current thread. This is useful when different threads need different executor configurations.
§Thread Safety
The thread-local executor is only accessible from the thread that set it. Each thread can have its own thread-local executor.
§Examples
use some_global_executor::Executor;
let executor = Executor::new("thread-local".to_string(), 2);
executor.set_as_thread_executor();
// This thread now uses this executor for thread-local spawning
// via some_executor::thread_executor::spawn()
Trait Implementations§
Source§impl AsMut<Executor> for ExecutorDrain
Provides mutable access to the underlying Executor from an ExecutorDrain.
impl AsMut<Executor> for ExecutorDrain
Provides mutable access to the underlying Executor from an ExecutorDrain.
This allows operations like resizing the thread pool even while draining.
§Examples
use some_global_executor::{Executor, ExecutorDrain};
let executor = Executor::new("test".to_string(), 2);
let mut drain = executor.drain_async();
drain.as_mut().resize(4); // Resize while drainingSource§impl AsRef<Executor> for ExecutorDrain
Provides immutable access to the underlying Executor from an ExecutorDrain.
impl AsRef<Executor> for ExecutorDrain
Provides immutable access to the underlying Executor from an ExecutorDrain.
§Examples
use some_global_executor::{Executor, ExecutorDrain};
let executor = Executor::new("test".to_string(), 2);
let drain = executor.drain_async();
let name = drain.as_ref().name();
assert_eq!(name, "test");Source§impl From<Executor> for ExecutorDrain
Converts an Executor into an ExecutorDrain future.
impl From<Executor> for ExecutorDrain
Converts an Executor into an ExecutorDrain future.
This conversion is equivalent to calling Executor::drain_async().
§Examples
use some_global_executor::{Executor, ExecutorDrain};
let executor = Executor::new("test".to_string(), 2);
let drain: ExecutorDrain = executor.into();
// In async context: drain.await;
// For this example, convert back and drain:
let executor: Executor = drain.into();
executor.drain();Source§impl From<ExecutorDrain> for Executor
Converts an ExecutorDrain back to its underlying Executor.
impl From<ExecutorDrain> for Executor
Converts an ExecutorDrain back to its underlying Executor.
This is useful when you need to access the executor after initiating a drain operation but before it completes.
§Examples
use some_global_executor::{Executor, ExecutorDrain};
let executor = Executor::new("test".to_string(), 2);
let drain: ExecutorDrain = executor.into();
let executor: Executor = drain.into();
executor.drain();Source§fn from(drain: ExecutorDrain) -> Self
fn from(drain: ExecutorDrain) -> Self
Source§impl SomeExecutor for Executor
Implementation of the SomeExecutor trait from the some_executor framework.
impl SomeExecutor for Executor
Implementation of the SomeExecutor trait from the some_executor framework.
This implementation provides the core task spawning functionality, supporting both synchronous and asynchronous task submission with type-safe observers for monitoring task execution.