pub struct Runtime { /* private fields */ }Expand description
Main runtime for executing async tasks
The Runtime is the central coordination point for the Luminal async runtime. It provides methods for spawning tasks, blocking on futures, and managing the runtime itself. Unlike tokio, this runtime is safe to pass across DLL boundaries as it doesn’t rely on thread-local storage.
Implementations§
Source§impl Runtime
impl Runtime
Sourcepub fn new() -> Result<Self, RuntimeError>
pub fn new() -> Result<Self, RuntimeError>
Creates a new Luminal runtime
This initializes a new multi-threaded runtime with a work-stealing scheduler using the number of available CPU cores. The runtime will create worker threads and begin processing the task queue immediately.
§Returns
A new Runtime instance wrapped in a Result
§Errors
Returns an error if the runtime could not be initialized
§Example
use luminal::Runtime;
let rt = Runtime::new().unwrap();
rt.block_on(async {
println!("Running on Luminal runtime!");
});Sourcepub fn spawn<F>(&self, future: F) -> JoinHandle<F::Output> ⓘ
pub fn spawn<F>(&self, future: F) -> JoinHandle<F::Output> ⓘ
Spawns a future onto the runtime
This method takes a future and begins executing it on the runtime, returning a JoinHandle that can be used to await its completion and retrieve its result.
§Type Parameters
F- The future type
§Parameters
future- The future to execute
§Returns
A JoinHandle that can be used to await the future’s completion
§Example
use luminal::Runtime;
let rt = Runtime::new().unwrap();
let handle = rt.spawn(async {
// Some async work
42
});
let result = rt.block_on(handle); // Waits for the result
assert_eq!(result, 42);Sourcepub fn block_on<F>(&self, future: F) -> F::Output
pub fn block_on<F>(&self, future: F) -> F::Output
Blocks the current thread until the provided future completes
This method takes a future and blocks the current thread until it completes, helping process other tasks while waiting to avoid deadlocks.
§Type Parameters
F- The future type
§Parameters
future- The future to execute and wait for
§Returns
The output of the future
§Example
use luminal::Runtime;
let rt = Runtime::new().unwrap();
let result = rt.block_on(async {
// Some async work
42
});
assert_eq!(result, 42);Sourcepub fn handle(&self) -> Handle
pub fn handle(&self) -> Handle
Returns a Handle to this runtime
The Handle provides a lightweight way to interact with the runtime without cloning the entire Runtime.
§Returns
A new Handle to this runtime
§Example
use luminal::Runtime;
let rt = Runtime::new().unwrap();
let handle = rt.handle();
// Use the handle to spawn tasks
let task = handle.spawn(async { 42 });Sourcepub fn stats(&self) -> (usize, usize)
pub fn stats(&self) -> (usize, usize)
Returns statistics about the runtime
§Returns
A tuple containing the current queue length and the number of tasks processed
§Example
use luminal::Runtime;
let rt = Runtime::new().unwrap();
let (queue_len, tasks_processed) = rt.stats();
println!("Queue length: {}, Tasks processed: {}", queue_len, tasks_processed);Trait Implementations§
Source§impl Clone for Runtime
impl Clone for Runtime
Source§fn clone(&self) -> Self
fn clone(&self) -> Self
Creates a new runtime referring to the same executor
This allows for lightweight cloning of the runtime, as the underlying executor is reference-counted.
§Returns
A new Runtime instance referring to the same executor
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more