How would you implement producer consumer in Swift 5.5?

I'm coming from a background in Golang and Rust. In those languages I would often spawn long-running tasks that generate messages to be consumed. I'm specifically referring to Golang channels and Rust's mpsc.

I was thinking that you might implement it as an Actor but some features that would desirable don't seem to fit into the Actor model.

Tokio, a library for Rust, has an mpsc implementation that has a try_send function. This is a synchronous function that errors out when the queue is full. This is useful for queuing messages when its unacceptable to block and when messages can be dropped. Does anyone have any thoughts for how something like this could be implemented in Swift?

1 Like

These are closest to AsyncSequence. A concrete producer would be pretty close to AsyncStream. The consume side is the iteration.


Cool. I didn't realise AsyncSequence and AsyncStream would fit that role but I can see how they could be used for that. Thanks :slight_smile:

Terms of Service

Privacy Policy

Cookie Policy