Search code examples
rustchannelrust-tokio

How to handle sending and receiving multiple values between 2 tasks?


I need to send and receive multiple values between 2 tasks. I am currently using tokio oneshot channel because I am only dealing with 2 tasks. But I can't seem to re-use the tx probably due to its one message limit. How is this situation handled usually?

  • Do I create a new oneshot channel everytime? or is there a way to reuse the channel?
  • Do I try packing all my interactions into 1 message and just do it once? -> seems very restrictive.
  • Do I use other channel types?

Solution

  • The tokio::sync::mpsc channel should be used in this situation.

    I suspect you're worried by the fact that it supports multiple senders, but its perfectly fine to use it when you only have one sender.