Expand description
Global executor management for program-wide task execution.
This module provides functionality for setting and accessing a global executor that persists for the entire lifetime of the program. This is useful when you need to spawn tasks from contexts where you cannot easily pass an executor as a parameter, such as signal handlers or global initialization code.
§Overview
The global executor pattern allows you to:
- Set a single executor for the entire program using
set_global_executor - Access this executor from anywhere using
global_executor - Spawn tasks without needing to thread an executor through your call stack
§Important Considerations
- The global executor can only be set once. Attempting to set it multiple times will panic.
- You must initialize the global executor before attempting to use it. Accessing
an uninitialized global executor will return
None. - For most use cases,
crate::current_executor::current_executoris preferred as it provides more flexibility and context-aware executor selection.
§Example Usage
use some_executor::global_executor::{set_global_executor, global_executor};
use some_executor::DynExecutor;
// Initialize the global executor early in your program
let executor: Box<DynExecutor> = todo!(); // Your executor implementation
set_global_executor(executor);
// Later, from anywhere in your program
global_executor(|e| {
if let Some(executor) = e {
// Use the executor
let mut executor = executor.clone_box();
// spawn tasks...
}
});Functions§
- global_
executor - Accesses the global executor through a callback function.
- set_
global_ executor - Sets the global executor for the entire program.