Async actor runs now

This commit is contained in:
Gregor Reitzenstein 2020-12-02 11:46:46 +01:00
parent f4148d398f
commit 8e3b8834c0
3 changed files with 13 additions and 6 deletions

View File

@ -26,7 +26,7 @@ pub struct Actor<S: Signal> {
inner: Option<S>,
actuator: Box<dyn Actuator + Send + Sync>,
future: Option<Box<dyn Future<Output=()> + Unpin + Send>>,
future: Option<BoxFuture<'static, ()>>,
}
impl<S: Signal + Unpin> Actor<S> {
@ -80,7 +80,7 @@ impl<S: Signal<Item=MachineState> + Unpin> Future for Actor<S> {
}
},
Some(Poll::Ready(Some(state))) => {
this.actuator.apply(state);
this.future.replace(this.actuator.apply(state));
Poll::Pending
}
}

View File

@ -151,11 +151,16 @@ fn maybe(matches: clap::ArgMatches, log: Arc<Logger>) -> Result<(), Error> {
// TODO HERE: Spawn all actors & inits
// Like so
ex.spawn(actor);
let t = ex.spawn(actor);
let (signal, shutdown) = async_channel::bounded::<()>(1);
easy_parallel::Parallel::new()
.each(0..4, |_| smol::block_on(ex.run(shutdown.recv())));
.each(0..4, |_| smol::block_on(ex.run(shutdown.recv())))
.run();
smol::block_on(t);
let db = db::Databases::new(&log, &config)?;
// TODO: Spawn api connections on their own (non-main) thread, use the main thread to

View File

@ -6,6 +6,7 @@ use smol::lock::RwLock;
use std::pin::Pin;
use futures::ready;
use futures::prelude::*;
use futures::future::BoxFuture;
use futures::channel::mpsc;
use futures::task::{Context, Poll, Spawn};
use futures_signals::signal::Signal;
@ -15,13 +16,14 @@ use crate::db::machine::MachineState;
use std::collections::HashMap;
pub trait Actuator {
fn apply(&mut self, state: MachineState);
fn apply(&mut self, state: MachineState) -> BoxFuture<'static, ()>;
}
pub struct Dummy;
impl Actuator for Dummy {
fn apply(&mut self, state: MachineState) {
fn apply(&mut self, state: MachineState) -> BoxFuture<'static, ()> {
println!("New state for dummy actuator: {:?}", state);
Box::pin(smol::future::ready(()))
}
}