Skip to main content

Module ch05_how_to_proceed

Module ch05_how_to_proceed 

Source
Expand description

Chapter 5: How to proceed.

You now know the core loop of every simu model: spawn processes, await timeouts / events / resources, run, read out results. The rest of the toolbox, in the order you are likely to need it:

  • PriorityResource — like Resource, but request(priority) serves lower numbers first (FIFO within a level). Triage queues, VIP lanes.
  • PreemptiveResource — a priority pool where an urgent request can evict a lower-priority holder mid-service; the victim observes it via guard.preempted(). See the type docs for the full pattern.
  • Container — continuous quantity instead of discrete units: tanks, silos, blood banks. put(amount) / get(amount) with strict FIFO waiters.
  • all_of! — the dual of any_of!: wait for every sub-future (barrier / fork-join).
  • Randomnessh.rng() borrows the env’s seeded RNG; combine with rng::sample or rand_distr for stochastic arrival/service times. Sample before .await — the borrow cannot be held across a suspension point.
  • Monte Carlomonte_carlo::run executes one full, independent simulation per seed in parallel threads:
use simu::{SimEnv, monte_carlo};

let end_times = monte_carlo::run(0..8u64, |seed| {
    let mut env = SimEnv::with_seed(seed);
    let h = env.handle();
    env.spawn(async move { h.timeout(1.0).await; });
    env.run();
    env.now()
});
assert_eq!(end_times.len(), 8); // results arrive in seed order

When you are ready for full models, three commented showcases combine everything above, each with a walkthrough document in examples/:

  • cargo run --example hospital — ER with priority triage, bed eviction, and a blood bank (PriorityResource, PreemptiveResource precursor patterns, Container).
  • cargo run --example brewery — a fermentation line with contamination events and cleanup priorities (EventTrigger, PriorityResource).
  • cargo run --example warehouse — a forklift fleet shared between receiving and shipping (PreemptiveResource end-to-end).

Coming from SimPy? The repository root has llms.txt with a complete SimPy → simu translation table.