Reputation: 9426
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.
Upvotes: 4
Views: 544
Reputation: 10683
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.
Upvotes: 3
Reputation: 84379
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 go
s 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 go
s. 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 go
s 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.
Upvotes: 7