✨ valkey task distribution
This commit is contained in:
parent
e827faaa3f
commit
f381f30e27
7 changed files with 866 additions and 26 deletions
76
examples/work_union.rs
Normal file
76
examples/work_union.rs
Normal file
|
@ -0,0 +1,76 @@
|
|||
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(4)]
|
||||
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 args: Vec<String> = std::env::args().collect();
|
||||
|
||||
match args.get(1).unwrap().as_str() {
|
||||
"serve" => {
|
||||
let mut s = ServiceManager::new().mode(comrade::service::ServiceMode::Decay);
|
||||
s = multiply_init_union(s);
|
||||
s = myfn_init_union(s);
|
||||
s = take_time_init_union(s);
|
||||
let s = s.spawn();
|
||||
|
||||
log::info!("Spawned workers. Working for 1 minute");
|
||||
|
||||
std::thread::sleep(Duration::from_secs(60));
|
||||
|
||||
myfn_shutdown();
|
||||
multiply_shutdown();
|
||||
take_time_shutdown();
|
||||
|
||||
s.join().unwrap();
|
||||
}
|
||||
"use" => {
|
||||
myfn_register_union();
|
||||
multiply_register_union();
|
||||
take_time_register_union();
|
||||
|
||||
do_work(multiply_Scoped {}, myfn_Scoped {});
|
||||
|
||||
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());
|
||||
}
|
||||
_ => {
|
||||
println!("Unknown command");
|
||||
}
|
||||
};
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue