Crate muse_reactor
source ·Expand description
§muse-reactor
muse-reactor provides a multi-threaded runtime that executes Muse code.
It’s primary features include:
- Spawning tasks across a threadpool
- Waiting on a task’s completion
- Cancelling a task
- Budgeting tasks in pools
§Basic Usage
use muse_reactor::Reactor;
use muse_lang::runtime::value::{Primitive, RootedValue};
// Create a new reactor for tasks to run in.
let reactor = Reactor::spawn();
// Spawn a task that computes 1 + 2
let task = reactor.spawn_source("1 + 2").unwrap();
// Wait for the result and verify it's 3.
assert_eq!(
task.join().unwrap(),
RootedValue::Primitive(Primitive::Int(3))
);TaskHandle is also a future that can be awaited to wait for the task to
complete.
§Budget Pools
Budget pools enable efficiently restricting groups of tasks to execution budgets. Each time a task assigned to a budget pool exhausts its budget, it requests additional budget from the pool. If no budget is available, the task is put to sleep and will automatically be resumed when the budget has been replenished.
use muse_reactor::{BudgetPoolId, BudgetPoolConfig, Reactor};
use muse_lang::runtime::value::{Primitive, RootedValue};
use std::time::Duration;
// Create a new reactor for tasks to run in.
let reactor = Reactor::spawn();
// Create a budget pool that we can spawn tasks within.
let pool = reactor.create_budget_pool(BudgetPoolConfig::default()).unwrap();
// Spawn a task within the budget pool
let task = pool.spawn_source("var i = 0; while i < 100 { i = i + 1; }; i").unwrap();
// Verify the task isn't able to complete.
assert!(task.join_for(Duration::from_secs(1)).is_none());
// Allocate enough budget.
pool.increase_budget(1_000);
// Wait for the task to complete
assert_eq!(
task.join().unwrap(),
RootedValue::Primitive(Primitive::Int(100))
);Structs§
- The settings for a budget pool in a
Reactor. - A handle to a budget pool.
- The unique identifier of a budget pool in a
Reactor. - A builder for a
Reactor. - A multi-threaded executor for Muse workloads.
- A handle to a spawned
Reactor<Work>]. - An operation could not be completed because the reactor is not running.
- A handle to a task spawned in a reactor.
Enums§
- A
WorkUnitthat can not be instantiated. - An error while preparing and executing code.
- An error waiting for a task to execute.