78 lines
1.6 KiB
Rust
78 lines
1.6 KiB
Rust
use std::time::Duration;
|
|
|
|
use comrade::{
|
|
job::{JobDispatcher, JobOrder, LabelPendingTaskIterator},
|
|
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(4)]
|
|
pub fn multiply(a: i32, b: i32) -> i32 {
|
|
a * b
|
|
}
|
|
|
|
pub fn batch_work() {
|
|
let mut work = Vec::new();
|
|
|
|
for i in 0..10 {
|
|
work.push((i.to_string(), multiply_async(i, i)));
|
|
}
|
|
|
|
for (label, res) in LabelPendingTaskIterator(work) {
|
|
println!("Finished task {label} -> {res}");
|
|
}
|
|
}
|
|
|
|
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 = multiply_init(s);
|
|
let s = take_time_init(s);
|
|
let s = s.spawn();
|
|
|
|
let x = myfn(55);
|
|
println!("myfn {x}");
|
|
|
|
batch_work();
|
|
|
|
// 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();
|
|
}
|