Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can ClojureScript's implementation of core.async be used in Clojure?

Is it possible to use ClojureScript's state machine-based implementation of core.async in Clojure, rather than Clojure's thread-based implementation? I'd like to be able to use core.async on the JVM but without using threads.

like image 498
exupero Avatar asked Dec 25 '22 13:12

exupero


1 Answers

There is no way to use core.async on the JVM in a strictly single-threaded fashion, unless you're willing to reach into the internals and replace the threadpool used for gos with one that only uses a single thread.

However, as edbond points out in his comment, the Clojure version of core.async does use state machines for handling gos. These state machines are then run on threads from a thread pool whose size is limited to double the number of processors + 42, so it is possible to launch thousands of gos without using as many real threads.

The JVM core.async also provides a thread macro that works like go, but launches real threads, plus a collection of double-bang operations (<!!, >!! etc.) that work like their single-bang counterparts, but in a blocking fashion. Whether you use them is up to you; if you stick to go and the single-bang family of operations, core.async will never launch any threads beyond the above-mentioned threadpool limit.

like image 79
Michał Marczyk Avatar answered May 12 '23 11:05

Michał Marczyk