Question

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.

Was it helpful?

Solution 2

It's not currently possible, but there's nothing stopping you from modifying core.async to support a single threaded model. All the dispatching is handled via clojure.core.async.impl.dispatch/run Change that function to use some sort of other dispatch method, and things should just work. The ClojureScript version of core.async has a different version of dispatch/run that uses setTimeout (or other things that might be faster). Copy that code, and modify it to work on your VM and it shouldn't be that hard of a change.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top