r/Clojure • • May 31 '25
Using channels for concurrency
Hi everyone, I've recently read about channels and the go function in clojure for concurrency. I have some experience with go, and as such I find this solution to concurrency quite intuitive. However, I was wondering if it's really used in practice or there are different solutions that are more idiomatic?
23
Upvotes
14
u/thheller May 31 '25
For Clojure I use core.async pretty much exclusively for my concurrency needs. I very rarely use the
gomacro though. The blocking ops are nicer and don't have the limitationsgohas. Channels are no doubt very useful and idiomatic.For CLJS on the other hand I never use core.async at all. The JS world pretty much settled on using promises. While interop with that is possible, I never liked the overhead it brings.
10
u/regular_hammock May 31 '25
There is absolutely nothing wrong with using core.async channels for concurrency, I would say it's quite idiomatic.
Sorry if I'm stating the obvious here, but it's also idiomatic to try and keep the concurrency stuff confined to the connective tissue of your app if you know what I mean. You'll want your business logic to live in plain old, data in, data out functions that know nothing about channels, and then use channels where needed to do the plumbing between those pure functions and the concurrent world.
I haven't taken the time to look more deeply into core.async.flow, but it looked very promising in that regard, maybe you'll want to have a look.
5
u/pauseless May 31 '25
I went from learning core.async first to Go (the language) - both for work projects. Go was honestly a breath of fresh air, for highly concurrent problems, after hitting many pain points with core.async. I understand that core.async has improved a lot, but ultimately, having a language designed around the concurrency model, just meant that Go had it nailed from day 1.
Ultimately, any function anywhere can pause any “thread”/goroutine in Go and it doesn’t matter. Writing core.async code can be a bit ‘infectious’, until you stop it with a blocking channel receive/send.
Anyway, take with a pinch of salt - it’s been a while and I haven’t been following current plans re virtual threads.
3
u/Marutks May 31 '25 edited May 31 '25
We use core.async in our microservices. Basically it is a library that helps to write Redis pub/sub event handlers in Clojure. It was written 10 years ago 👍. There is no async.flow or Redis Streams 🤷♂️.
3
u/Marutks May 31 '25
Does anyone use core.async instead of thread pools?
1
u/Marutks May 31 '25
We used to have some core.async in our frontend code. I think re-frame is more idiomatic for cljs.
1
u/joinr May 31 '25
I use it, in addition to the existing ref types. I think I use core.async more often than refs, but less often than atoms. Futures (and/or promises) + reference types + immutable structures get you a long way, an they're built in. I also leverage java.util.concurrent sometimes. Lots of options. core.async meshes nicely with the above though IMO. I have also used it in cljs.