use async_runtime::{Priority, RuntimeBuilder};
use futures_lite::future;
use std::num::NonZeroUsize;
use std::thread;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let runtime = RuntimeBuilder::new(NonZeroUsize::new(2).expect("non-zero")).build()?;
let parent = runtime.spawn(Priority::Normal, {
let nested = runtime.spawner();
async move {
let parent_worker = thread::current().id();
let mut children = Vec::new();
for _ in 0..16 {
children.push(
nested
.spawn(Priority::Normal, async { thread::current().id() })
.expect("runtime is still running"),
);
}
let mut ran_on_parent = 0;
for child in children {
if child.await == parent_worker {
ran_on_parent += 1;
}
}
(parent_worker, ran_on_parent)
}
})?;
let (parent_worker, ran_on_parent) = future::block_on(parent);
println!("parent ran on {parent_worker:?}; {ran_on_parent}/16 children also ran there");
println!("local enqueue is preferred; cross-worker stealing remains allowed");
runtime.shutdown_graceful()?;
Ok(())
}