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
How to tell when a channel is disconnected? #302
Comments
The standard and simplest way to do this is to simply use another channel. You can send a message after your work is done, thereby alerting your stats thread that it can quit. (You don't even need to send a message, you can just drop this signaling channel since receives on a disconnected channel never block.) |
Exactly what @BurntSushi said, but I'll just elaborate a little bit... Create a channel whose disconnection signals that worker threads have completed their work: let (done, quit) = bounded(0); Give each worker thread a clone of Then, in the stats thread, you can do something like this: let ticker = tick(Duration::from_secs(60));
loop {
select! {
recv(ticker) -> _ => STAT.print(),
recv(quit) -> _ => break,
}
} When all clones of |
Yeah, I was generally aware of this pattern, among a collection of other options. Thanks for the |
This is really the idiomatic approach :) Disconnection is only used to inform send/recv methods that the other side of the channel went away. It's not really a flag intended to be polled independently of blocking methods. To make an analogy with iterators, this is kind of similar to how iterators get exhausted once (There exist peekable iterators, but they're more like plumbing a channel to a peekable |
Closing the issue. If you have any other questions, I'd be happy to help! |
I think this is a bit unfortunate. I have code that receives some data from a channel, and sends it on another one. Ideally I could do something like this:
Adding another channel just to send a "closed" signal seems inelegant and a fragile duplication of information - I now have to do extra work to make sure that the "closed" signal is sent iff Perhaps it is simply too difficult to implement? |
I would like an
is_connected()
/is_disconnected()
method on channels.In a simple app, with some workers cloning the rx of a channel, and the main thread putting work into the channel, the channel is disconnected once the main thread drops the send side. The workers continue draining the queue until they get
None
when the queue is empty and disconnected. All works as intended.However, I have another thread as well, and this makes the termination condition more complex. That thread is printing progress statistics. I want it to exit when the queue is empty and disconnected; the app can then wait on just this thread, rather than join on all the workers as well. But at present it can only determine when the queue is empty.
Further discussion and example code here: https://users.rust-lang.org/t/test-for-when-crossbeam-channel-is-disconnected/23554
It looks like this idea has come up before; it's mentioned in #236
The text was updated successfully, but these errors were encountered: