How to select channels?

#1

I checked the mpsc::Select which is deprecated and nightly only. What is the rust way of handling multiple signals if there’s no select?

#2

You should use crossbeam instead: https://github.com/crossbeam-rs/crossbeam

2 Likes
#3

does this mean rust’s own std channels are all together deprecated ?

#4

Select for mpsc won’t happen, so there is a proposal to replace stdlib’s implementation with crossbeam’s (and/or deprecate):

#5

That makes sense because i cant just imagine using channels without select

#6

It’s interesting how different people’s usages are. I’ve used mpsc in at least three different projects, and never needed select.

#7

How did you handle kill signals or timeouts ?

#8

For simple utilities I use ctrlc crate and timeouts built into HTTP clients. For bigger ones Actix has handling of these built-in (plus Futures composed with timers).

#9

i meant internally how did you handle those. For instance you have something that is listening to a channel you may need to tell it to stop and gracefully return

#10

If the sending side is destroyed, the receiver stops listening automatically.

It’s also usual to send enum Message { Do(Stuff), Exit }.

#11

im coming from golang background in go often you have goroutines waiting for data from one channel or context.Done and you can possibly have more channels for different sorts of things and you use select for this
I guess this is not a rusty use case but still its a valid one :slight_smile:

#12

@kornel actually yes the enum makes sense i always forget how powerfull rust enums are

#13


@kornel here i have a small example of go way :slight_smile: so in rust this could be done in a typesafe way using enums :slight_smile: +1 for rust then :smiley:

#14

Yeah, channels do a lot of work in Go. In Rust that’s more specific, e.g. you’d use Rayon’s parallel iterators or scopes to process multiple things in parallel and wait for them. Some waiting can be arranged around mutexes, etc.

2 Likes