✨ iterated fn + defer + retry
This commit is contained in:
parent
c010cc13e8
commit
18c663fcdb
11 changed files with 678 additions and 65 deletions
12
examples/defer.rs
Normal file
12
examples/defer.rs
Normal file
|
@ -0,0 +1,12 @@
|
|||
use comrade::defer;
|
||||
|
||||
fn main() {
|
||||
println!("Hello World!");
|
||||
|
||||
defer!(|| {
|
||||
// this will run at the end of the scope
|
||||
println!("Bye World!");
|
||||
});
|
||||
|
||||
println!("doing something");
|
||||
}
|
67
examples/iterated.rs
Normal file
67
examples/iterated.rs
Normal file
|
@ -0,0 +1,67 @@
|
|||
use std::time::Duration;
|
||||
|
||||
use comrade::iterated::{FunctionContext, IteratedFunction};
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct AccFnContext {
|
||||
pub iter: u64,
|
||||
pub acc: u64,
|
||||
}
|
||||
|
||||
pub fn multiply_iterated(
|
||||
mut ctx: FunctionContext<AccFnContext, (u64, u64), u64>,
|
||||
) -> FunctionContext<AccFnContext, (u64, u64), u64> {
|
||||
// init
|
||||
let (a, b) = ctx.initial;
|
||||
|
||||
// end condition (return)
|
||||
if b == ctx.context.iter {
|
||||
ctx.done(ctx.context.acc);
|
||||
return ctx;
|
||||
}
|
||||
|
||||
// computation
|
||||
println!("doing calc {}", ctx.context.acc);
|
||||
std::thread::sleep(Duration::from_millis(50));
|
||||
let val = ctx.context.acc + a;
|
||||
|
||||
// saving state
|
||||
ctx.state(|x| {
|
||||
x.iter += 1;
|
||||
x.acc = val;
|
||||
});
|
||||
|
||||
return ctx;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
env_logger::init();
|
||||
|
||||
let f: IteratedFunction<AccFnContext, (u64, u64), u64> =
|
||||
IteratedFunction::new(multiply_iterated);
|
||||
|
||||
let x = f.run_to_end((5, 4));
|
||||
println!("computed x -> {x}");
|
||||
|
||||
// async
|
||||
let f = IteratedFunction::new_threaded(multiply_iterated, (5, 5));
|
||||
println!("This is running");
|
||||
println!("result is {}", f.output());
|
||||
|
||||
let f = IteratedFunction::new_threaded(multiply_iterated, (5, 50));
|
||||
|
||||
// pause the function
|
||||
f.pause();
|
||||
|
||||
// stop the function and get state
|
||||
let state = f.stop();
|
||||
println!("This was running");
|
||||
println!("state is {state:?}");
|
||||
|
||||
println!("reviving function");
|
||||
// continue with previous computed state
|
||||
let f = IteratedFunction::new_threaded_from_state(multiply_iterated, state);
|
||||
|
||||
// get output
|
||||
println!("result is {}", f.output());
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
use std::time::Duration;
|
||||
|
||||
use comrade::{
|
||||
job::{JobDispatcher, JobOrder},
|
||||
job::{JobDispatcher, JobOrder, LabelPendingTaskIterator},
|
||||
service::ServiceManager,
|
||||
worker,
|
||||
};
|
||||
|
@ -22,6 +22,18 @@ 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);
|
||||
|
@ -41,17 +53,19 @@ fn main() {
|
|||
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!");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue