comrade/examples/work.rs
2025-03-07 21:20:25 +01:00

64 lines
1.3 KiB
Rust

use std::time::Duration;
use comrade::{
job::{JobDispatcher, JobOrder},
service::ServiceManager,
worker,
};
use crossbeam::channel::Receiver;
#[worker]
pub fn take_time(i: i32) {
std::thread::sleep(Duration::from_millis(i as u64));
}
#[worker]
pub fn myfn(i: i32) -> i32 {
i * 2
}
#[worker]
pub fn multiply(a: i32, b: i32) -> i32 {
a * b
}
fn do_work(multiply: multiply_Scoped, myfn: myfn_Scoped) {
for i in 0..10 {
let x = multiply.call(i, i);
println!("myfn {i} -> {x}");
let x = myfn.call(i);
println!("myfn {i} -> {x}");
}
}
fn main() {
env_logger::init();
let s = ServiceManager::new().mode(comrade::service::ServiceMode::Decay);
let (s, multiply) = multiply_init_scoped(s);
let (s, myfn_fn) = myfn_init_scoped(s);
let s = s.spawn();
do_work(multiply, myfn_fn);
s.join().unwrap();
let s = ServiceManager::new().mode(comrade::service::ServiceMode::Decay);
let s = myfn_init(s);
let s = take_time_init(s);
let s = s.spawn();
let x = myfn(55);
println!("myfn {x}");
// decoupled
let e = take_time_async(1500);
println!("This will run right after!");
println!("the value is {}", e.wait());
myfn_shutdown();
take_time_shutdown();
s.join().unwrap();
}