-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question regarding usage in a non-trivial case #25
Comments
Hey! First of all, thanks for your interest in this library :) I wrote it for myself and am happy to see that other people find it useful too. To address your questions: .start("Alpha", Alpha::new(tx).run) But I got problems with the lifetimes. I might revisit it though, maybe I find a way. About shutdown order: The order is unspecified, it could happen in any order. I would best describe it as "simultaneously", as much as that is possible in an asynchronous system. I just realized there is no |
Yea, because of that lifetime issue I ended up with that |
Actually, I think the I think either the |
Sure, here's one of them in a simplified version: pub struct AudioPlayer {
rx: mpsc::Receiver<Play>,
stream_handle: OutputStreamHandle,
}
impl AudioPlayer {
pub fn new(rx: mpsc::Receiver<Play>, stream_handle: OutputStreamHandle) -> Self {
Self {
rx,
stream_handle,
}
}
pub async fn run(&mut self, subsys: SubsystemHandle) -> Result<()> {
tokio::select! {
_ = subsys.on_shutdown_requested() => {
info!("Audio player shutting down");
},
res = self.process() => res?
}
Ok(())
}
async fn process(&mut self) -> Result<()> {
while let Some(play) = self.rx.recv().await {
// …
}
Ok(())
}
} |
Replace |
Indeed that does seem to compile without issues, neat :). As I said, I'm still relatively new to rust, I'm not perfectly sure when I need to pass self as a mutable reference and when just as a mutable :D |
What I dislike a little bit is that I have to do the tx cloning all in the main function outside of the closures. Do you have some design pattern which might move the cloning process? Maybe have a function for each subsystem to add it to the top level, borrows the tx and creates it own clone? |
The difference is that the owner changes. In, |
I'm having an open pull request I just wrote based on your suggestion, here: #26 This would allow you to write:
Is that something you would find useful? |
Hell yes, that looks a lot cleaner to me 👍 |
I assume that there's no real issue in rust having the original |
You could leave out the '.clone()' in the last subsystem, passing the original object into it |
Good point! Thanks a lot for your input, that's really helpful :) |
Welcome :) |
Published as 0.7.0. Let me know how it worked for you :) |
Trying it out right now, I get this error message from the compiler: Here's my implementation of the trait: #[async_trait::async_trait]
impl IntoSubsystem<Error> for AudioPlayer {
async fn run(mut self, subsys: SubsystemHandle) -> Result<()> {
tokio::select! {
_ = subsys.on_shutdown_requested() => {
info!("Audio player shutting down");
},
res = self.process() => res?
}
Ok(())
}
} ( Nevermind, I forgot to install |
Let's talk about this on the tokio discord. Just PM me. I think I will need to see more code to find the issue, I don't think it's in the code you posted |
@DASPRiD Just saw that you forgot to install |
Yes you can, thanks :) |
Hi there! First off, thanks for this amazing project, it really helped simplifying my previous setup with handling shutdowns manually!
I tried to look for ways how people use this library in real world applications, but I couldn't really find much. I have a relatively simple question, so maybe you can guide me in the right direction:
Say I have 4 subsystems attached to the top level. Three of those need to send messages to the first one. Where would you create
tx
and how would you pass it down? At the moment I'm creating all of them in mymain
function and pass them into thenew()
function of every subsystem before callingrun(subsys)
on them. This looks a little bit like this, and I'm wondering if there's a cleaner solution (to be fair, I'm relatively new to rust):In general, it'd be really nice to see a more complete example with subsystem intercommunication in the example directory I guess :)
Another question which popped into my mind, which I couldn't find an answer for: When a subsystem has nested subsystems and a shutdown is requested, do the nested subsystems shut down first before the parent, or is the order not guaranteed?
Thanks a lot for your time!
The text was updated successfully, but these errors were encountered: