pub struct Immediate;Expand description
Immediate execution strategy.
This strategy executes a given task immediately, and is primarily intended for testing and debugging purposes. No threading is involved, which makes it much easier to reason about execution order. Additionally, it can act as a baseline for comparison with worker-based strategies.
Warning: When channels are involved, this strategy might deadlock, i.e., when a task from a worker thread tries to send on a bounded channel that is full. In this case, make sure to either use unbounded channels, or actively drain the channel before sending on it.
§Examples
use zrx_executor::strategy::{Immediate, Strategy};
// Create strategy and submit task
let strategy = Immediate::default();
strategy.submit(Box::new(|| println!("Task")))?;Implementations§
Trait Implementations§
Source§impl Strategy for Immediate
impl Strategy for Immediate
Source§fn submit(&self, task: Box<dyn Task>) -> Result
fn submit(&self, task: Box<dyn Task>) -> Result
Submits a task.
This method immediately executes the given Task, so that it runs on
the main thread, which is primarily intended for testing and debugging.
Note that tasks are intended to only run once, which is why they are consumed. If a task needs to be run multiple times, it must be wrapped in a closure that creates a new task each time. This allows for safe sharing of state between tasks.
§Errors
This method is infallible, and will always return Ok.
Errors
§Examples
use zrx_executor::strategy::{Immediate, Strategy};
// Create strategy and submit task
let strategy = Immediate::default();
strategy.submit(Box::new(|| println!("Task")))?;Source§fn num_workers(&self) -> usize
fn num_workers(&self) -> usize
Returns the number of workers.
The number of usable workers is always 1 for this strategy, as tasks
are immediately executed on the main thread when submitted.
§Examples
use zrx_executor::strategy::{Immediate, Strategy};
// Get number of workers
let strategy = Immediate::new();
assert_eq!(strategy.num_workers(), 1);Source§fn num_tasks_running(&self) -> usize
fn num_tasks_running(&self) -> usize
Returns the number of running tasks.
The number of running tasks is always 0 for this strategy, as tasks
are immediately executed on the main thread when submitted.
§Examples
use zrx_executor::strategy::{Immediate, Strategy};
// Get number of running tasks
let strategy = Immediate::default();
assert_eq!(strategy.num_tasks_running(), 0);Source§fn num_tasks_pending(&self) -> usize
fn num_tasks_pending(&self) -> usize
Returns the number of pending tasks.
The number of pending tasks is always 0 for this strategy, as tasks
are immediately executed on the main thread when submitted.
§Examples
use zrx_executor::strategy::{Immediate, Strategy};
// Get number of pending tasks
let strategy = Immediate::default();
assert_eq!(strategy.num_tasks_pending(), 0);