<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Clojure Q&amp;A - Recent questions tagged core.async</title>
<link>https://ask.clojure.org/index.php/tag/core.async</link>
<description></description>
<item>
<title>How to use core.async from cljr command line tool?</title>
<link>https://ask.clojure.org/index.php/14766/how-to-use-core-async-from-cljr-command-line-tool</link>
<description>&lt;p&gt;At some point it would be good to facilitate use of core.async from cljr command line tool (clr.core.cli).&lt;/p&gt;
&lt;p&gt;I think what's required is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;add a deps-clr.edn to &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/tools.analyzer&quot;&gt;clojure/tools.analyzer&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;add a deps-clr.edn to &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/tools.analyzer.clr&quot;&gt;clojure/tools.analyzer.clr&lt;/a&gt; (depends on tools.analyzer, clr.tools.reader, clr.core.memoize -&amp;gt; clr.core.cache -&amp;gt; clr.data.priority-map)&lt;/li&gt;
&lt;li&gt;add a deps-clr.edn to &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/clr.core.async&quot;&gt;clojure/clr.core.async&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;also, archive &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/clr.tools.analyzer&quot;&gt;clojure/clr.tools.analyzer&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is not urgent because it's possible to procure core.async from NuGet but it would be convenient if it could also be procured via git.&lt;/p&gt;
</description>
<category>ClojureCLR</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14766/how-to-use-core-async-from-cljr-command-line-tool</guid>
<pubDate>Mon, 24 Nov 2025 06:05:40 +0000</pubDate>
</item>
<item>
<title>`pipeline-blocking` uses `thread` internally instead of `io-thread`</title>
<link>https://ask.clojure.org/index.php/14738/pipeline-blocking-uses-thread-internally-instead-of-thread</link>
<description>&lt;p&gt;&lt;code&gt;pipeline-blocking&lt;/code&gt; is explicitly meant for blocking operations (implying I/O). However, internally it uses &lt;code&gt;thread&lt;/code&gt; rather than &lt;code&gt;io-thread&lt;/code&gt; for processing: &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/core.async/blob/master/src/main/clojure/clojure/core/async.clj#L637-L640&quot;&gt;https://github.com/clojure/core.async/blob/master/src/main/clojure/clojure/core/async.clj#L637-L640&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Since 1.9.829-alpha2 this has practical implications, as only &lt;code&gt;io-thread&lt;/code&gt; will utilize virtual threads, when available.&lt;/p&gt;
&lt;p&gt;Can this be fixed / changed?&lt;br&gt;
If not, can an explicit &lt;code&gt;pipeline-io&lt;/code&gt; (or similar)  function be added to support this use case?&lt;/p&gt;
&lt;p&gt;Thanks&lt;/p&gt;
</description>
<category>core.async</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14738/pipeline-blocking-uses-thread-internally-instead-of-thread</guid>
<pubDate>Wed, 29 Oct 2025 15:54:00 +0000</pubDate>
</item>
<item>
<title>Choosing between core.async pipelines</title>
<link>https://ask.clojure.org/index.php/14732/choosing-between-core-async-pipelines</link>
<description>&lt;p&gt;It's no longer clear what kind of pipeline is suitable for what kind of task in &lt;code&gt;core.async&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Previously, my understanding was that anything I/O-bound should go into &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/core.async/blob/v1.9.829-alpha2/src/main/clojure/clojure/core/async.clj#L682-L686&quot;&gt;&lt;code&gt;pipeline-blocking&lt;/code&gt;&lt;/a&gt;, while CPU-bound tasks went into regular (i.e. &lt;code&gt;:compute&lt;/code&gt;) &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/core.async/blob/v1.9.829-alpha2/src/main/clojure/clojure/core/async.clj#L665-L680&quot;&gt;&lt;code&gt;pipeline&lt;/code&gt;&lt;/a&gt;. But a while back &lt;code&gt;:compute&lt;/code&gt; pipelines were &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/core.async/commit/3429e3e1f1d49403bf9608b36dbd6715ffe4dd4f&quot;&gt;changed&lt;/a&gt; to use threads instead of go-blocks. And now, with the introduction of &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojure.org/news/2025/10/01/async_virtual_threads&quot;&gt;virtual threads&lt;/a&gt;, &lt;code&gt;pipeline-async&lt;/code&gt; seems like the right choice for I/O-bound tasks.&lt;/p&gt;
&lt;p&gt;It seems that workloads are being classified into one of &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/core.async/blob/v1.9.829-alpha2/src/main/clojure/clojure/core/async/impl/dispatch.clj#L130-L132&quot;&gt;three types&lt;/a&gt;: &lt;code&gt;:compute&lt;/code&gt;, &lt;code&gt;:io&lt;/code&gt;, and &lt;code&gt;:mixed&lt;/code&gt;. Is &lt;code&gt;:async&lt;/code&gt; meant to be the equivalent of &lt;code&gt;:io&lt;/code&gt;, and &lt;code&gt;:blocking&lt;/code&gt; the equivalent of &lt;code&gt;:mixed&lt;/code&gt;? I don't think that's original semantics, but it seems to be the mapping now.&lt;/p&gt;
</description>
<category>core.async</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14732/choosing-between-core-async-pipelines</guid>
<pubDate>Sun, 26 Oct 2025 22:19:12 +0000</pubDate>
</item>
<item>
<title>Replace core.async’s internal LinkedList queue with ArrayDeque?</title>
<link>https://ask.clojure.org/index.php/14696/replace-core-asyncs-internal-linkedlist-queue-arraydeque</link>
<description>&lt;p&gt;Hi! While reading the channel implementation I noticed the internal queue is a &lt;code&gt;java.util.LinkedList&lt;/code&gt;:&lt;br&gt;
&lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/core.async/blob/master/src/main/clojure/clojure/core/async/impl/channels.clj&quot;&gt;https://github.com/clojure/core.async/blob/master/src/main/clojure/clojure/core/async/impl/channels.clj&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The common JVM guidance these days is to prefer &lt;code&gt;java.util.ArrayDeque&lt;/code&gt; for FIFO/LIFO queues due to better locality and lower GC overhead. For example, the JDK docs state: “This class is likely to be faster than Stack when used as a stack, and faster than LinkedList when used as a queue.”&lt;br&gt;
&lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.oracle.com/javase/8/docs/api/java/util/ArrayDeque.html&quot;&gt;https://docs.oracle.com/javase/8/docs/api/java/util/ArrayDeque.html&lt;/a&gt;&lt;br&gt;
Related SO discussion: &lt;a rel=&quot;nofollow&quot; href=&quot;https://stackoverflow.com/questions/6163166/why-is-arraydeque-better-than-linkedlist&quot;&gt;https://stackoverflow.com/questions/6163166/why-is-arraydeque-better-than-linkedlist&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;A few questions for the maintainers:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Was &lt;code&gt;LinkedList&lt;/code&gt; originally chosen for a specific reason (e.g., very old JDK compatibility)?&lt;/li&gt;
&lt;li&gt;Are there behaviors in channels that specifically rely on &lt;code&gt;LinkedList&lt;/code&gt; (e.g., allowing null, I believe channels disallow nil anyway, or particular iterator characteristics), or would &lt;code&gt;ArrayDeque&lt;/code&gt; be a drop-in replacement for the add/remove-from-ends operations used?&lt;/li&gt;
&lt;li&gt;If we provide a small PR and benchmark showing an improvement (lower allocations / better throughput under contention), would such a change be considered?&lt;/li&gt;
&lt;li&gt;Since &lt;code&gt;ArrayDeque&lt;/code&gt; is available on Java 8+, and current Clojure/core.async baselines target that or newer, is there any remaining compatibility concern?&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Context: there was an older conversation in Clojurians (#clojure) that suggested ArrayDeque could reduce GC pressure under load and that the original choice may have been influenced by older JDKs:&lt;br&gt;
&lt;a rel=&quot;nofollow&quot; href=&quot;https://clojurians.slack.com/archives/C03S1KBA2/p1526551888000376&quot;&gt;https://clojurians.slack.com/archives/C03S1KBA2/p1526551888000376&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;If this sounds reasonable, I’m happy to run the core.async test suite, add JMH-style benchmarks focused on channel put/take hot paths, and submit a PR.&lt;/p&gt;
&lt;p&gt;Thanks!&lt;/p&gt;
</description>
<category>core.async</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14696/replace-core-asyncs-internal-linkedlist-queue-arraydeque</guid>
<pubDate>Tue, 02 Sep 2025 08:02:18 +0000</pubDate>
</item>
<item>
<title>Typos in Flow guide</title>
<link>https://ask.clojure.org/index.php/14686/typos-in-flow-guide</link>
<description>&lt;p&gt;As reported by a &lt;a rel=&quot;nofollow&quot; href=&quot;https://news.ycombinator.com/item?id=44936309&quot;&gt;Hacker News user&lt;/a&gt;, there are typos in the &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojure.github.io/core.async/flow-guide.html&quot;&gt;Flow Guide&lt;/a&gt;  :&lt;/p&gt;
&lt;p&gt;&quot;excepiton&quot; should be &quot;exception&quot;&lt;br&gt;
&quot;provded&quot; should be &quot;provided&quot;&lt;/p&gt;
&lt;p&gt;and also, it seems that in the transition section, &quot;The description arity takes the current state ...&quot; should be &quot;The transition arity takes the current state ...&quot;&lt;/p&gt;
</description>
<category>core.async</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14686/typos-in-flow-guide</guid>
<pubDate>Mon, 18 Aug 2025 08:19:35 +0000</pubDate>
</item>
<item>
<title>Is core.async.flow available in ClojureScript?</title>
<link>https://ask.clojure.org/index.php/14633/is-core-async-flow-available-in-clojurescript</link>
<description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I know it's early days for core.async.flow but I thought I would give it a try in one of my ClojureScript projects.&lt;/p&gt;
&lt;p&gt;I've got &lt;code&gt;{:mvn/version &quot;1.9.808-alpha1&quot;}&lt;/code&gt; in my deps.edn but I'm getting the following error when I try to access the namespace:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;No such namespace: clojure.core.async.flow, could not locate clojure/core/async/flow.cljs&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Thanks for your help.&lt;/p&gt;
</description>
<category>core.async</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14633/is-core-async-flow-available-in-clojurescript</guid>
<pubDate>Thu, 17 Jul 2025 08:31:10 +0000</pubDate>
</item>
<item>
<title>How best to call long-running tasks from a core.async.flow process?</title>
<link>https://ask.clojure.org/index.php/14621/how-best-call-long-running-tasks-from-core-async-flow-process</link>
<description>&lt;p&gt;I'd like to write to a database as the final process in a core.async.flow flow. The db load can take several minutes. If I do this work directly in the :transform function's thread, flow-monitor can't ping that process for the duration of the load. But if I offload the work to a different thread and return from the :transform function, I can't as easily control back pressure from that process.&lt;/p&gt;
&lt;p&gt;The relevant process currently looks something like:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(def db-loader
  (flow/map-&amp;gt;step
   {:describe (fn [] {:workload :io
                      :ins {:in &quot;Batched items&quot;}})
    :init     (fn [state] state)
    :transition (fn [state status] state)
    :transform (fn [state _ batch]
                 (t/log! &quot;DB Loader is loading a batch.&quot;)
                 (Thread/sleep 10000) ;; imagine this code writes to the db.
                 (t/log! &quot;DB Loader loaded a batch.&quot;)
                 [state])}))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A more complete code example is at &lt;a rel=&quot;nofollow&quot; href=&quot;https://gist.github.com/tomconnors/245fb69ed757b34502c8d57637db8de2&quot;&gt;https://gist.github.com/tomconnors/245fb69ed757b34502c8d57637db8de2&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Is there a better option for this process?&lt;/p&gt;
</description>
<category>Clojure</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14621/how-best-call-long-running-tasks-from-core-async-flow-process</guid>
<pubDate>Wed, 09 Jul 2025 18:31:41 +0000</pubDate>
</item>
<item>
<title>what kind of systems could be made with clojure.async.flow?</title>
<link>https://ask.clojure.org/index.php/14526/what-kind-of-systems-could-be-made-with-clojure-async-flow</link>
<description>&lt;p&gt;Trying to capture mechanical essence of &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojure.github.io/core.async/flow.html&quot;&gt;clojure.async.flow&lt;/a&gt; with basic terms instead of flow own terms i guess that clojure.async.flow allows to structure a system as a set of stateful functions running concurrently in the same jvm process and interacting with each other explicitly via channels.&lt;/p&gt;
&lt;p&gt;what kind of systems could be made with clojure.async.flow?&lt;/p&gt;
&lt;p&gt;it's definitely not naive service which handles API http requests, transforms it somehow and puts it into database.&lt;/p&gt;
</description>
<category>core.async</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14526/what-kind-of-systems-could-be-made-with-clojure-async-flow</guid>
<pubDate>Tue, 29 Apr 2025 15:48:35 +0000</pubDate>
</item>
<item>
<title>`doseq` and `for` expands body twice</title>
<link>https://ask.clojure.org/index.php/14433/doseq-and-for-expands-body-twice</link>
<description>&lt;p&gt;The bodies of &lt;code&gt;doseq&lt;/code&gt; and &lt;code&gt;for&lt;/code&gt; seem to be duplicated in their expansions.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ clojure
user=&amp;gt; (defmacro a [] (prn :expand))
#'user/a
user=&amp;gt; (doseq [_ nil] (a))
:expand
:expand
nil
user=&amp;gt; (for [_ nil] (a))
:expand
:expand
()
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I found the same problem in ClojureScript. I could not find an existing discussion about this so I'm not sure if this is by design, apologies if so. I'm aware that these macros have a large code footprint and that &lt;code&gt;doseq&lt;/code&gt; cannot use closures (the usual way to prevent exponential expansion), but I missed this detail.&lt;/p&gt;
&lt;p&gt;This leads to exponential code blowup if these forms are nested. Artificial demonstration:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ clojure
Clojure 1.12.0
user=&amp;gt; (def counter (atom -1))
#'user/counter
user=&amp;gt; (defmacro a [] (prn :expand (swap! counter inc)))
#'user/a
user=&amp;gt; #(doseq [_ 1] (doseq [_ 2] (doseq [_ 3] (doseq [_ 4] (a)))))
:expand 0
:expand 1
:expand 2
:expand 3
:expand 4
:expand 5
:expand 6
:expand 7
:expand 8
:expand 9
:expand 10
:expand 11
:expand 12
:expand 13
:expand 14
:expand 15
#object[user$eval926$fn__927 0x76563d26 &quot;user$eval926$fn__927@76563d26&quot;]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This might be especially relevant to core.async, where placing &lt;code&gt;go&lt;/code&gt; under &lt;code&gt;doseq&lt;/code&gt; is idiomatic, since &lt;code&gt;go&lt;/code&gt; is expensive in both expansion time and code size.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;user=&amp;gt; (time (eval '(doseq [_ nil] (a/go (doseq [_ nil] (a/go (doseq [_ nil] (a/go))))))))
&quot;Elapsed time: 837.230917 msecs&quot;
nil
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It also leads to duplicated reflection warnings from the fully expanded forms, which is how I found this problem.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;user=&amp;gt; (doseq [_ nil] (Thread/sleep (identity 1)))
Reflection warning, NO_SOURCE_PATH:1:16 - call to static method sleep on java.lang.Thread can't be resolved (argument types: unknown).
Reflection warning, NO_SOURCE_PATH:1:16 - call to static method sleep on java.lang.Thread can't be resolved (argument types: unknown).
nil
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I believe the exponential expansion for &lt;code&gt;doseq&lt;/code&gt; was introduced in Clojure 1.1.0 with support for chunked seqs with &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/clojure/commit/1abb7a56de1678321054af7fce183184f06974dd&quot;&gt;this commit&lt;/a&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ clojure -Sdeps '{:deps {org.clojure/clojure {:mvn/version &quot;1.0.0&quot;}}}' 
Downloading: org/clojure/clojure/1.0.0/clojure-1.0.0.pom from central
Downloading: org/clojure/clojure/1.0.0/clojure-1.0.0.jar from central
Clojure 1.0.0-
user=&amp;gt; (defmacro a [] (prn :expand))
#'user/a
user=&amp;gt; (doseq [_ nil] (a))
:expand
nil
user=&amp;gt; ^D
$ clojure -Sdeps '{:deps {org.clojure/clojure {:mvn/version &quot;1.1.0&quot;}}}'
Downloading: org/clojure/clojure/1.1.0/clojure-1.1.0.pom from central
Downloading: org/clojure/clojure/1.1.0/clojure-1.1.0.jar from central
Clojure 1.1.0
user=&amp;gt; (defmacro a [] (prn :expand))
#'user/a
user=&amp;gt; (doseq [_ nil] (a))
:expand
:expand
nil
user=&amp;gt; 
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;At first I wasn't sure if this was hopeless, but I think &lt;code&gt;doseq&lt;/code&gt; is fixable fusing the chunked and non-chunked cases like so:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defmacro doseq
  &quot;Repeatedly executes body (presumably for side-effects) with
  bindings and filtering as provided by \&quot;for\&quot;.  Does not retain
  the head of the sequence. Returns nil.
  
  Unlike clojure.core/doseq, does not cause exponential macro expansion
  of expressions in bindings or body.&quot;
  [seq-exprs &amp;amp; body]
  (#'clojure.core/assert-args
     (vector? seq-exprs) &quot;a vector for its binding&quot;
     (even? (count seq-exprs)) &quot;an even number of forms in binding vector&quot;)
  (let [step (fn step [recform exprs]
               (if-not exprs
                 [true `(do ~@body)]
                 (let [k (first exprs)
                       v (second exprs)]
                   (if (keyword? k)
                     (let [steppair (step recform (nnext exprs))
                           needrec (steppair 0)
                           subform (steppair 1)]
                       (cond
                         (= k :let) [needrec `(let ~v ~subform)]
                         (= k :while) [false `(when ~v
                                                ~subform
                                                ~@(when needrec [recform]))]
                         (= k :when) [false `(if ~v
                                               (do
                                                 ~subform
                                                 ~@(when needrec [recform]))
                                               ~recform)]))
                     (let [seq- (gensym &quot;seq_&quot;)
                           chunk- (with-meta (gensym &quot;chunk_&quot;)
                                             {:tag 'clojure.lang.IChunk})
                           count- (gensym &quot;count_&quot;)
                           i- (gensym &quot;i_&quot;)
                           in-chunk- (gensym &quot;in-chunk_&quot;)
                           recform `(if ~in-chunk-
                                      (recur ~seq- ~chunk- ~count- (unchecked-inc ~i-))
                                      (recur (next ~seq-) nil 0 0))
                           steppair (step recform (nnext exprs))
                           needrec (steppair 0)
                           subform (steppair 1)]
                       [true
                        `(loop [~seq- (seq ~v), ~chunk- nil,
                                ~count- 0, ~i- 0]
                           (let [~in-chunk- (&amp;lt; ~i- ~count-)
                                 ~seq- (if ~in-chunk- ~seq- (seq ~seq-))]
                             (when (if ~in-chunk- true ~seq-)
                               (let [chunked?# (if ~in-chunk- false (chunked-seq? ~seq-))
                                     ~k (if ~in-chunk-
                                          (.nth ~chunk- ~i-)
                                          (if chunked?# nil (first ~seq-)))]
                                 (if (if ~in-chunk- false chunked?#)
                                   (let [c# (chunk-first ~seq-)]
                                     (recur (chunk-rest ~seq-) c#
                                            (int (count c#)) (int 0)))
                                   (do ~subform
                                       ~@(when needrec [recform])))))))])))))]
    (nth (step nil (seq seq-exprs)) 1)))
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>Macros</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14433/doseq-and-for-expands-body-twice</guid>
<pubDate>Wed, 26 Feb 2025 23:15:06 +0000</pubDate>
</item>
<item>
<title>core.async 1.8 beta1 cached thread pools can grow to hundreds of threads when doing io work</title>
<link>https://ask.clojure.org/index.php/14428/core-async-beta1-cached-thread-pools-hundreds-threads-doing</link>
<description>&lt;p&gt;Hi, I've been testing the new clojure.core.async 1.8.711-beta1, and suspected that since the default fixed thread pool was replaced with a cached thread pool, it could be possible for the system to create a very large number of OS threads, in the order of hundreds or thousands depending on the size of the workload.&lt;/p&gt;
&lt;p&gt;I am wondering if this was intended or if there might be a better middle-ground such as still using a CachedThreadPool but potentially accepting maximum sizes for each pool type via sys props, with reasonable defaults.&lt;/p&gt;
&lt;p&gt;Example code below:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(ns user)

(require '[clojure.core.async :as a]
         '[clojure.core.async.impl.dispatch :as d])

(defn do-work
  &quot;Simulate some async work which &quot;
  [task-count task-timeout]
  (a/go-loop [c (a/merge
                 (doall
                  (for [i (range task-count)]
                    (a/go
                      ;;; this simulates non-blocking park insdie the go-block
                      (a/&amp;lt;! (a/timeout
                             (rand-int task-timeout)))
                      ;;; this simulates blocking IO inside an io-thread
                      (a/&amp;lt;! (a/io-thread
                             (Thread/sleep
                              ^int (rand-int task-timeout))))
                      i))))]
    (let [v (a/&amp;lt;! c)]
      (if (nil? v)
        {:pool-size (.getPoolSize ^java.util.concurrent.ThreadPoolExecutor (d/executor-for :core-async-dispatch))}
        (recur c)))))

(def active
  (atom false))

(defn run-sim
  []
  (reset! active true)
  (a/go-loop [iter 1]
    (let [start (System/currentTimeMillis)
          {:keys [pool-size]} (a/&amp;lt;! (do-work
                                     2000  ;;; simulated number of tasks
                                     100)) ;;; simulated max delay in ms
          stop (System/currentTimeMillis)
          elapsed (- stop start)]
      (println &quot;Iteration:&quot; iter &quot;Pool Size:&quot; pool-size &quot;Elapsed:&quot; elapsed)
      (when (and @active
                 (&amp;lt; iter 10))
        (recur (inc iter))))))

(defn stop-sim
  []
  (reset! active false))

(comment
  (run-sim)   ;;; run the sim
  (stop-sim)) ;;; stop the sim
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>Libs</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14428/core-async-beta1-cached-thread-pools-hundreds-threads-doing</guid>
<pubDate>Wed, 26 Feb 2025 18:54:58 +0000</pubDate>
</item>
<item>
<title>core.async go block error with clojure 1.12 method values</title>
<link>https://ask.clojure.org/index.php/14425/core-async-go-block-error-with-clojure-1-12-method-values</link>
<description>&lt;p&gt;I haven't seen this reported. &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(Long/.toString 123) ;;works
(a/&amp;lt;!! (a/thread (Long/.toString 123))) ;;works
(a/go (Long/.toString 123)) ;;error
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;Note: The following stack trace applies to the reader or compiler, your code was not executed.

CompilerException Syntax error macroexpanding a/go at (1:1). {:clojure.error/phase :macro-syntax-check, :clojure.error/line 1, :clojure.error/column 1, :clojure.error/source &quot;NO_SOURCE_PATH&quot;, :clojure.error/symbol a/go}
ExceptionInfo No matching method: .toString for class: class java.lang.Long and arity: 1 {:method .toString, :class java.lang.Long, :argc 1, :file &quot;NO_SOURCE_PATH&quot;, :column 7, :line 1}
	clojure.tools.analyzer.passes.jvm.validate/validate-call (validate.clj:127)
	clojure.tools.analyzer.passes.jvm.validate/validate-call (validate.clj:87)
	clojure.tools.analyzer.passes.jvm.validate/eval4802/fn--4803 (validate.clj:137)
	clojure.lang.MultiFn.invoke (MultiFn.java:229)
	clojure.tools.analyzer.passes.jvm.validate/validate (validate.clj:265)
	clojure.tools.analyzer.passes.jvm.validate/validate (validate.clj:240)
	clojure.lang.Var.invoke (Var.java:386)
	clojure.tools.analyzer.passes/compile-passes/fn--3424/fn--3429 (passes.clj:167)
	clojure.tools.analyzer.passes/compile-passes/fn--3424/fn--3431 (passes.clj:169)
	clojure.tools.analyzer.passes/compile-passes/fn--3424/fn--3431 (passes.clj:169)
	clojure.tools.analyzer.passes/compile-passes/fn--3424/fn--3431 (passes.clj:169)
	clojure.core/partial/fn--5929 (core.clj:2648)
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>core.async</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14425/core-async-go-block-error-with-clojure-1-12-method-values</guid>
<pubDate>Tue, 25 Feb 2025 19:17:23 +0000</pubDate>
</item>
<item>
<title>Somehow I have to loop after all</title>
<link>https://ask.clojure.org/index.php/13479/somehow-i-have-to-loop-after-all</link>
<description>&lt;p&gt;I know some little Lisp, but Clojure feels a bit different:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn mojorate []
  (let [v (vec (range 1 11))
        max (- (count v) 1)
        atomix (atom [])
        chanl (async/chan)]
    (println v)
    (loop [i 0]
      (async/go
        (&amp;gt;! chanl (swap! atomix conj (/ (get v i) 2))))
      (if (= i max)
        (do (&amp;lt;!! (async/go (&amp;lt;! chanl)))
          (async/close! chanl)
          @atomix)
        (recur (inc i))))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It's just an exercise with channel, atom loop/recur etc.&lt;/p&gt;
&lt;p&gt;Can you do it like this?&lt;/p&gt;
</description>
<category>Refs, agents, atoms</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13479/somehow-i-have-to-loop-after-all</guid>
<pubDate>Tue, 21 Nov 2023 19:50:16 +0000</pubDate>
</item>
<item>
<title>Why do core.async timeouts rely on non-monotonic system clock?</title>
<link>https://ask.clojure.org/index.php/13420/why-do-core-async-timeouts-rely-on-non-monotonic-system-clock</link>
<description>&lt;p&gt;&lt;code&gt;clojure.core.async/timeout&lt;/code&gt; uses &lt;code&gt;System/currentTimeMillis&lt;/code&gt; to calculate its deadline (see &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/core.async/blob/322adc72852bb79c143ebe5d203d100534fda8f2/src/main/clojure/clojure/core/async/impl/timers.clj#L29&quot;&gt;here&lt;/a&gt; and &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/core.async/blob/322adc72852bb79c143ebe5d203d100534fda8f2/src/main/clojure/clojure/core/async/impl/timers.clj#L62&quot;&gt;here&lt;/a&gt;). While &lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/System.html#currentTimeMillis()&quot;&gt;the docstring for &lt;code&gt;System/currentTimeMillis&lt;/code&gt;&lt;/a&gt; doesn't mention it explicitly, it's quite clearly unsuitable for this purpose because it returns the current time as reported by the OS. Since that time can change arbitrarily (e.g. by correction via NTP), the deadline calculations may end up inconsistent. This behavior is very much intended, see e.g. &lt;a rel=&quot;nofollow&quot; href=&quot;https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8061480&quot;&gt;this ticket&lt;/a&gt; where it was suggested to switch &lt;code&gt;System/currentTimeMillis&lt;/code&gt; over to a monotonic clock where possible. It was refused with the following comment:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;System.currentTimeMillis() is required to return milliseconds since the epoch and should reflect external changes to the system/wall-clock time. CLOCK_MONOTONIC_COARSE does not provide that and so can not be used for this purpose.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Also, the &lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/concurrent/ScheduledExecutorService.html&quot;&gt;docstring for &lt;code&gt;java.util.concurrent.ScheduledExecutorService&lt;/code&gt;&lt;/a&gt; states this quite plainly:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;All schedule methods accept relative delays and periods as arguments, not absolute times or dates. It is a simple matter to transform an absolute time represented as a Date to the required form. For example, to schedule at a certain future date, you can use: schedule(task, date.getTime() - System.currentTimeMillis(), TimeUnit.MILLISECONDS). Beware however that expiration of a relative delay need not coincide with the current Date at which the task is enabled due to network time synchronization protocols, clock drift, or other factors.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Since all we care about here is deltas, it seems advisable to switch to &lt;code&gt;System/nanoTime&lt;/code&gt; instead. Unfortunately, &lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/System.html#nanoTime()&quot;&gt;its docstring&lt;/a&gt; &lt;em&gt;also&lt;/em&gt; doesn't make its properties very obvious. It doesn't mention that it's a monotonic clock but only hints at it by stating:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;Returns the current value of the running Java Virtual Machine's high-resolution time source, in nanoseconds. This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. [...] The values returned by this method become meaningful only when the difference between two such values, obtained within the same instance of a Java virtual machine, is computed.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;A case in point for using it is OpenJDK's implementation of &lt;code&gt;java.util.concurrent.ScheduledThreadPoolExecutor&lt;/code&gt; whose &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/openjdk/jdk/blob/c86592d38d651beac40f1da43c718a2d4b17bd19/src/java.base/share/classes/java/util/concurrent/ScheduledThreadPoolExecutor.java#L524-L530&quot;&gt;deadline calculation method does so&lt;/a&gt;, as well as its &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/openjdk/jdk/blob/c86592d38d651beac40f1da43c718a2d4b17bd19/src/java.base/share/classes/java/util/concurrent/ScheduledThreadPoolExecutor.java#L244-L246&quot;&gt;&lt;code&gt;getDelay&lt;/code&gt; implementation&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Furthermore--if it's anything to go by--various StackOverflow posts also discuss this and arrive at the same conclusion, e.g. &lt;a rel=&quot;nofollow&quot; href=&quot;https://stackoverflow.com/questions/510462/is-system-nanotime-completely-useless/54566928#54566928&quot;&gt;this one&lt;/a&gt;:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;Nonetheless, nanoTime() should still be preferred for implementing timed blocking, interval waiting, timeouts, etc. to currentTimeMillis() because the latter is a subject to the &quot;time going backward&quot; phenomenon (e. g. due to server time correction), i. e. currentTimeMillis() is not suitable for measuring time intervals at all. See this answer for more information.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;See also &lt;a rel=&quot;nofollow&quot; href=&quot;https://ask.clojure.org/index.php/10306/core-cache-ttlcacheq-timer-is-not-monotonic?show=10306#q10306&quot;&gt;a similar question about &lt;code&gt;core.cache&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
</description>
<category>core.async</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13420/why-do-core-async-timeouts-rely-on-non-monotonic-system-clock</guid>
<pubDate>Wed, 01 Nov 2023 16:26:33 +0000</pubDate>
</item>
<item>
<title>Virtual thread executor for core.async</title>
<link>https://ask.clojure.org/index.php/13366/virtual-thread-executor-for-core-async</link>
<description>&lt;p&gt;There have been a few people on Slack asking about virtual threads and core.async before. Here is an example of one such thread &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojurians.slack.com/archives/C05423W6H/p1677676131782299&quot;&gt;https://clojurians.slack.com/archives/C05423W6H/p1677676131782299&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Of course, consideration is also needed for ClojureScript and non-Loom code. I would assume a virtual thread aware implementation of core.async would probably exist in a dedicated namespace to prevent breaking old code.&lt;/p&gt;
&lt;p&gt;I am posting this question both as a feature request and a central place for people to vote.&lt;/p&gt;
</description>
<category>core.async</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13366/virtual-thread-executor-for-core-async</guid>
<pubDate>Sun, 08 Oct 2023 23:30:28 +0000</pubDate>
</item>
<item>
<title>core.async: Add a function to set thread executor</title>
<link>https://ask.clojure.org/index.php/12692/core-async-add-a-function-to-set-thread-executor</link>
<description>&lt;p&gt;See slack #babashka discussion &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojurians.slack.com/archives/CLX41ASCS/p1677319666536479&quot;&gt;https://clojurians.slack.com/archives/CLX41ASCS/p1677319666536479&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The use case is setting a virtual thread pool executor with SCI.&lt;br&gt;
In Clojure I could set &lt;code&gt;clojure.core.async.impl.exec.threadpool/thread-pool-executor&lt;/code&gt;. &lt;/p&gt;
&lt;p&gt;Doing the intern in SCI will not have the desired effect since the pre-compiled functions don't see the change. &lt;/p&gt;
&lt;p&gt;Clojure.core agents also allow this via &lt;code&gt;set-agent-send-executor!&lt;/code&gt; etc. &lt;/p&gt;
</description>
<category>Libs</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12692/core-async-add-a-function-to-set-thread-executor</guid>
<pubDate>Sat, 25 Feb 2023 11:21:02 +0000</pubDate>
</item>
<item>
<title>Fix for the ASYNC-163 - Off by two error in pipeline-async</title>
<link>https://ask.clojure.org/index.php/12394/fix-for-the-async-163-off-by-two-error-in-pipeline-async</link>
<description>&lt;p&gt;Hello!&lt;/p&gt;
&lt;p&gt;I've been working on a fix for the &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojure.atlassian.net/browse/ASYNC-163&quot;&gt;ASYNC-163&lt;/a&gt; bug, and I think I've got a robust solution that doesn't require many changes to the &lt;code&gt;pipeline*&lt;/code&gt; function.&lt;/p&gt;
&lt;p&gt;I'm new to contributing to Clojure, so I'm a bit unsure about the process, though I've read about it on the clojure.org website. There's already an existing ticket for the problem, which I've linked above, however, it was closed as &quot;won't fix&quot;, and I don't have a Jira account to comment there and send a patch, so I've decided to post here.&lt;/p&gt;
&lt;p&gt;After studying the source code of the &lt;code&gt;pipeline*&lt;/code&gt; function, I've understood the whole process of how jobs are passed between go-threads and channels and found a simple fix, that requires adding one more channel to synchronize job-producer and result-consumer threads.&lt;br&gt;
I have a more in-depth explanation of the bug in my blog. It is rather long and convoluted, so I think it would be best if I will not repeat myself here, making this question unnecessarily long and much harder to understand. The explanation is available &lt;a rel=&quot;nofollow&quot; href=&quot;https://andreyorst.gitlab.io/posts/2022-11-21-clojures-coreasync-pipeline-async-off-by-two-error-explained/#explanation&quot;&gt;here&lt;/a&gt;. &lt;/p&gt;
&lt;p&gt;I'm attaching a patch here, feel free to point me in the right direction if this place is not for this type of question. I've tested the in the REPL with various scenarios and in one of my projects, that uses regular &lt;code&gt;pipeline-async&lt;/code&gt;, it works correctly. In addition, it passes tests in the &lt;code&gt;core.async&lt;/code&gt; library, which I ran with the &lt;code&gt;lein test&lt;/code&gt; command.&lt;/p&gt;
&lt;p&gt;Patch: &lt;a rel=&quot;nofollow&quot; href=&quot;https://andreyorst.gitlab.io/0001-fix-ASYNC-163.patch&quot;&gt;https://andreyorst.gitlab.io/0001-fix-ASYNC-163.patch&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;At the moment I have some problems with running CLJS tests with or without my patch for the async.cljs file, but once I figure out the problem I'll post an updated patch, for both JVM and CLJS runtimes&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Edit: Nov 25 2022&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Patch v2: &lt;a rel=&quot;nofollow&quot; href=&quot;https://andreyorst.gitlab.io/0001-fix-ASYNC-163-2.patch&quot;&gt;https://andreyorst.gitlab.io/0001-fix-ASYNC-163-2.patch&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This patch includes the same fix for async.cljs, though I wasn't able to run tests for some reason - I get the &lt;code&gt;Uncaught TypeError: process.on is not a function&lt;/code&gt; error in the browser. Not sure what I'm missing, the project's readme doesn't mention any requirements besides building with lein and opening the HTML file. But the fix is essentially the same, so it should work fine.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Edit Dec 08 2022&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I've put the fixed version of &lt;code&gt;pipeline*&lt;/code&gt; into a library &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/andreyorst/pipeline-extras&quot;&gt;pipeline-extras&lt;/a&gt;. It also has unordered versions of all pipelines, which should have higher throughput because the conveyer doesn't stop if any of the tasks take longer to complete than the other ones.&lt;/p&gt;
</description>
<category>core.async</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12394/fix-for-the-async-163-off-by-two-error-in-pipeline-async</guid>
<pubDate>Tue, 22 Nov 2022 17:48:51 +0000</pubDate>
</item>
<item>
<title>core.async readme oversight</title>
<link>https://ask.clojure.org/index.php/12372/core-async-readme-oversight</link>
<description>&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/core.async/blob/96cb49e15b046e17e5b73eee5f9ce61ab44883ba/README.md?plain=true#L17&quot;&gt;https://github.com/clojure/core.async/blob/96cb49e15b046e17e5b73eee5f9ce61ab44883ba/README.md?plain=true#L17&lt;/a&gt; refers to 1.5, while the latest at the moment is 1.6&lt;/p&gt;
</description>
<category>core.async</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12372/core-async-readme-oversight</guid>
<pubDate>Wed, 09 Nov 2022 10:55:02 +0000</pubDate>
</item>
<item>
<title>core.async mult test is passing accidentally</title>
<link>https://ask.clojure.org/index.php/12100/core-async-mult-test-is-passing-accidentally</link>
<description>&lt;p&gt;Think I’ve found a bug in core.async tests, the following test for mult working seems purely coincidental:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;;; ASYNC-127 
(let [ch (to-chan! [1 2 3])
      m (mult ch)
      t-1 (chan)
      t-2 (chan)
      t-3 (chan)]
  (tap m t-1)
  (tap m t-2)
  (tap m t-3)
  (close! t-3)
  (is (= 1 (&amp;lt;!! t-1)))
  (is (= nil (a/poll! t-1))) ;; t-2 hasn't taken yet
  (is (= 1 (&amp;lt;!! t-2)))
  (is (= 2 (&amp;lt;!! t-1))) ;; now available
  (is (= nil (a/poll! t-1)))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The docstring of core.async/mult states that: Items received when there are no taps get dropped. Following this statement, the 3 items put by to-chan! should be dropped, as when the mult is created, it would start consuming them without taps yet on the mult.My suspicion is that it passes because the go’s created inside mult and to-chan! might be slow enough to create so that the taps get executed before the mult starts consuming.&lt;/p&gt;
&lt;p&gt;Does this make sense? Am I missing something?&lt;/p&gt;
&lt;p&gt;EDIT: Found this issue &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojure.atlassian.net/browse/ASYNC-246&quot;&gt;https://clojure.atlassian.net/browse/ASYNC-246&lt;/a&gt; which seems to be related, in that case probably hangs because the items might be properly dropped and then &lt;code&gt;(&amp;lt;!! t-1)&lt;/code&gt; hangs&lt;/p&gt;
</description>
<category>Clojure</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12100/core-async-mult-test-is-passing-accidentally</guid>
<pubDate>Wed, 03 Aug 2022 17:37:17 +0000</pubDate>
</item>
<item>
<title>Literal `()` erroneously produces `nil` in cljs.core.async `go` blocks</title>
<link>https://ask.clojure.org/index.php/11520/literal-erroneously-produces-nil-in-cljs-core-async-blocks</link>
<description>&lt;pre&gt;&lt;code&gt;(go (println () [] (range 0) (list) {} #{}))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This prints&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;nil [] () () {} #{}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But it should print (which it properly does outside of go blocks)&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;() [] () () {} #{}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This appears to only be an issue in ClojureScript.&lt;/p&gt;
</description>
<category>ClojureScript</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/11520/literal-erroneously-produces-nil-in-cljs-core-async-blocks</guid>
<pubDate>Sat, 29 Jan 2022 19:26:41 +0000</pubDate>
</item>
<item>
<title>Clojurescript core.async go block incorrectly handles records returned from tagged literals</title>
<link>https://ask.clojure.org/index.php/10987/clojurescript-incorrectly-handles-records-returned-literals</link>
<description>&lt;p&gt;In clojurescript, I have a data reader that returns a record from a tagged literal:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(ns x.x)

(defrecord X [y])

(defn make-x [y] (-&amp;gt;X y))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;data_readers.cljc:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;{x x.x/make-x}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This doesn't work correctly inside of go blocks:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(println &quot;outside of go:&quot; #x&quot;a&quot;)
(go
  (println &quot;inside of go:&quot; (&amp;lt;! (go #x&quot;b&quot;))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Results in:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;outside of go: #x.x.X{:y a}
inside of go: {:y b}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;@hiredman on the clojurians slack pointed out the issue:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;what is happening is here&lt;br&gt;
&lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/core.async/blob/master/src/main/clojure/cljs/core/async/impl/ioc_macros.clj#L403&quot;&gt;https://github.com/clojure/core.async/blob/master/src/main/clojure/cljs/core/async/impl/ioc_macros.clj#L403&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;blockquote&gt;&lt;p&gt;map? is returning true so it is compiling your record as a map&lt;/p&gt;
&lt;/blockquote&gt;
</description>
<category>ClojureScript</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10987/clojurescript-incorrectly-handles-records-returned-literals</guid>
<pubDate>Fri, 27 Aug 2021 18:53:31 +0000</pubDate>
</item>
<item>
<title>How to work with AsyncIterable Interface in CLJS</title>
<link>https://ask.clojure.org/index.php/10896/how-to-work-with-asynciterable-interface-in-cljs</link>
<description>&lt;p&gt;Does clojurescript has any way to work with AsyncIterable inteface in JS. The JS way is to use &lt;a rel=&quot;nofollow&quot; href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of&quot;&gt;for await of&lt;/a&gt; . &lt;br&gt;
For normal async/await or promise, I found - &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojurescript.org/guides/promise-interop&quot;&gt;https://clojurescript.org/guides/promise-interop&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I'm trying to build a IPFS file browser and its JS &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/ipfs/js-ipfs/blob/master/docs/core-api/FILES.md#ipfslsipfspath&quot;&gt;api&lt;/a&gt; returns AsyncIterable.&lt;/p&gt;
</description>
<category>ClojureScript</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10896/how-to-work-with-asynciterable-interface-in-cljs</guid>
<pubDate>Wed, 04 Aug 2021 03:10:13 +0000</pubDate>
</item>
<item>
<title>Strategies for diagnosing core.async code that is unexpectedly blocking the main thread?</title>
<link>https://ask.clojure.org/index.php/10190/strategies-diagnosing-async-unexpectedly-blocking-thread</link>
<description>&lt;p&gt;I'm new to core.async (and CSP more generally) and struggling to find good debugging strategies. In particular, I frequently fail to diagnosis which part of the code is infinitely blocking the main thread. I'll give a particular example:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(deftest passing-test
  (let [c (async/to-chan! [2 1 0 5 4])]
    (is (= [2 1 0 5 4]
           (async/&amp;lt;!! (async/into [] c))))))


(deftest hanging-test
  (let [[e o] (-&amp;gt;&amp;gt; (async/to-chan! [2 1 0 5 4])
                   (async/split even?))]
    (is (= [2 0 4]
           (async/&amp;lt;!! (async/into [] e))))
    (is (= [1 5]
           (async/&amp;lt;!! (async/into [] o))))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The first test passes and does not block the main thread. The second test hangs forever. The only difference is the addition of &lt;code&gt;async/split&lt;/code&gt; to route even and odd integers to their respective channels.&lt;/p&gt;
&lt;p&gt;My specific questions are as follows:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;p&gt;What explains the difference between the first and second tests with respect to blocking the main thread?&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What are some strategies to debugging core.async code that is unexpectedly blocking the main thread?&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What is the best way to write unit tests for code that creates and manipulates channels? Is &lt;code&gt;(async/&amp;lt;!! (async/into [] o))&lt;/code&gt; a reasonable way to collect the values flowing through a channel into a collection?&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
</description>
<category>core.async</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10190/strategies-diagnosing-async-unexpectedly-blocking-thread</guid>
<pubDate>Sun, 14 Feb 2021 21:07:06 +0000</pubDate>
</item>
<item>
<title>cljs core.async bound symbol `new` conflicts with fn `new`causing errors</title>
<link>https://ask.clojure.org/index.php/10135/cljs-core-async-bound-symbol-conflicts-with-causing-errors</link>
<description>&lt;p&gt;Hi there,&lt;/p&gt;
&lt;p&gt;it seems to me I found a bug in cljs core.async 1.3.610 (clj works well). Do you guys agree with me?&lt;/p&gt;
&lt;p&gt;The issue:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(cljs.core.async/go
  (println &quot;XXX before&quot;)     
  (let [old 1
      new „my-new&quot;]
   (println &quot;XXX after&quot;)
   (println [old new])))
=&amp;gt; „XXX vorher&quot;
=&amp;gt; „XXX nacher&quot;
=&amp;gt; TypeError TypeError: 1.call is not a function
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Changing the new symbol to xnew works:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(cljs.core.async/go
  (println &quot;XXX before&quot;)     
  (let [old 1
      xnew „my-new&quot;]
   (println &quot;XXX after&quot;)
   (println [old xnew])))
=&amp;gt; „XXX vorher&quot;
=&amp;gt; „XXX nacher&quot;
=&amp;gt; [1 „my-new&quot;]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The following snippet of macroexpanding shows the error I mean. The &lt;code&gt;new&lt;/code&gt; for instantiating &lt;code&gt;cljs.core/PersistentVector&lt;/code&gt; got resolved with the value I bound to &lt;code&gt;new&lt;/code&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;...
(clojure.core/let
 [inst_35971
 (println &quot;XXX before&quot;)
 inst_35972
 (. cljs.core/PersistentVector -EMPTY-NODE)
 inst_35973
 (js* &quot;[{},{}]&quot; 1 &quot;my-new&quot;)
 inst_35974
 (&quot;my-new&quot;
  cljs.core/PersistentVector
  nil
  2
  5
  inst_35972
  inst_35973
  nil)
 inst_35975
 (println inst_35974)
 state_35977
 …]
…))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So do not use &lt;code&gt;new&lt;/code&gt; as symbol! :-$&lt;/p&gt;
</description>
<category>ClojureScript</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10135/cljs-core-async-bound-symbol-conflicts-with-causing-errors</guid>
<pubDate>Mon, 01 Feb 2021 07:39:32 +0000</pubDate>
</item>
<item>
<title>Is &quot;port&quot; inside core.asyn documentation same as a &quot;chan&quot;?</title>
<link>https://ask.clojure.org/index.php/9987/is-port-inside-core-asyn-documentation-same-as-a-chan</link>
<description>&lt;p&gt;Inside the &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojure.github.io/core.async/&quot;&gt;documentation&lt;/a&gt; for &lt;code&gt;core.async&lt;/code&gt;, the identifier &lt;code&gt;port&lt;/code&gt; is used as an input to some functions/forms. Is &lt;code&gt;port&lt;/code&gt; the same as &lt;code&gt;chan&lt;/code&gt;? Or is it an interface or a protocol implemented by &lt;code&gt;chan&lt;/code&gt;?&lt;/p&gt;
</description>
<category>Docs</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9987/is-port-inside-core-asyn-documentation-same-as-a-chan</guid>
<pubDate>Tue, 29 Dec 2020 23:51:09 +0000</pubDate>
</item>
<item>
<title>How to do a blocking put on a channel ?</title>
<link>https://ask.clojure.org/index.php/9924/how-to-do-a-blocking-put-on-a-channel</link>
<description>&lt;p&gt;I am getting the  following error&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;No more than 1024 pending takes are allowed on a single channel&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;However, I though that if i defined a channel with a specifed size , the puts would  block.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(def event-chan (chan 10))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I am calling this in a loop&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(&amp;gt;!! event-chan evt)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;My expectation is that it would block is there were pending 10 items in the channel .. however does not seem to be happening. My code is reading a fifo queue in a database and proceesiing it. it seems to not block even if the items are pending in the channel&lt;/p&gt;
&lt;p&gt;  edit: updated to copy paste the full actual code.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn process-edn [{:keys [data code]}]
; lots db processing code here
)
  
(def event-chan (chan 10))
  
(defn eat-now-or-later[evt]
    (if ( = (evt :code) :loadevt)
        (do (println &quot;sync processing load event &quot;)
            (process-edn evt))
        (&amp;gt;!! event-chan evt)
      ))
  
  (defn async-deque [shutdown?]
    (time! dq-call-processing-time
           (let [k-t (ftup/from commons/*JOB_FIFO_EVENT_QUEUE*)
                 fdb (cfdb/select-api-version cfdb/clj-fdb-api-version)]
             (go (while true (process-edn (&amp;lt;! event-chan))))

             (loop[evt (with-open [^Database db (cfdb/open fdb)] (fifo/dequeue db k-t))]
              
              (if (or (shutdown?)  (nil? evt)) 0
                  
                  (do                   
                   (eat-now-or-later evt)  
                   (recur (with-open [^Database db (cfdb/open fdb)] (fifo/dequeue db k-t))))
                  )
              )))
    (ptasks/print-stats))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The above is the function definition. I am calling this function from the daemon code below&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(ns dqdaemon.core
  (:require [shutdown.core :as shutdown])
  (:require [com.videocloudmanager.petikaa.dequeue-tasks :as dqt])
 )

;; A crude approximation of your application's state.
(def state (atom {}))

(defn shutdown?[] (false? (:running @state)))

(defn init [args]
  (swap! state assoc :running true)
  (shutdown/add-hook! ::descriptive-name #(do
                                            (swap! state assoc :running false)         
                                            (println &quot;It's about to go down!&quot;)))
  )

(defn start []
  (while (:running @state)
    (println &quot;tick&quot;)
     (dqt/async-deque shutdown?)
    (Thread/sleep 2000)))


;; Enable command-line invocation
(defn -main [&amp;amp; args]
  (init args)
  (start))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;   &lt;strong&gt;Update:&lt;/strong&gt;&lt;br&gt;
 I understand that this is not an issue with blocking puts. It is because my code is creating unlimited goloops.&lt;/p&gt;
&lt;p&gt;This code is being called in a loop and the parent method of this loop , async-dequ, is being called in a forever loop. &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(go (while true (process-edn (&amp;lt;! event-chan)))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I am moving this code out of the loop-recur and into the main method  to initialize when the daemon is started&lt;/p&gt;
</description>
<category>core.async</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9924/how-to-do-a-blocking-put-on-a-channel</guid>
<pubDate>Tue, 08 Dec 2020 19:43:21 +0000</pubDate>
</item>
<item>
<title>(CLJS) finally-block prevents exception throw in go-block</title>
<link>https://ask.clojure.org/index.php/9587/cljs-finally-block-prevents-exception-throw-in-go-block</link>
<description>&lt;p&gt;ClojureScript 1.10.520, could not reproduce in Clojure. Reproduced in &lt;code&gt;core.async&lt;/code&gt; 1.3.610 and 1.1.582, not reproducible in 1.0.567.&lt;/p&gt;
&lt;p&gt;Looks the same as &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojure.atlassian.net/browse/ASYNC-198?focusedCommentId=12026&quot;&gt;https://clojure.atlassian.net/browse/ASYNC-198?focusedCommentId=12026&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Code:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(ns app.main
  (:require [clojure.core.async :as a]))

(a/go
  (try
    (prn :ret
         (try
           (throw (ex-info &quot;expected&quot; {}))

           (catch js/Error e
             (prn :catch1)
             (throw e))

           (finally
             (prn :finally))))

    (catch js/Error _e
      (prn :catch2))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Expected:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;:catch1
:finally
:catch2
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Actual:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;:catch1
:finally
:ret #error {:message &quot;expected&quot;, :data {}}
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>core.async</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9587/cljs-finally-block-prevents-exception-throw-in-go-block</guid>
<pubDate>Mon, 07 Sep 2020 12:47:54 +0000</pubDate>
</item>
<item>
<title>Infinite loop with stateful transducer on core.async channel</title>
<link>https://ask.clojure.org/index.php/9529/infinite-loop-with-stateful-transducer-core-async-channel</link>
<description>&lt;p&gt;I am trying to implement a simple stateful transducer that counts the number of items (with ClojureScript):&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn stateful-counter []
  (fn [xf]
    (let [counter (atom 0)]

      (fn
        ([] (xf))

        ([result]
         (xf (xf result @counter)))

        ([result _]
          (swap! counter inc)
          result)))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When running this on a sequence I get the output:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(into [] (stateful-counter) (range 5))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;[5]&lt;/p&gt;
&lt;p&gt;This is exactly what I expect.&lt;/p&gt;
&lt;p&gt;When running this on a core.async channel I get an infinite sequence of 5s:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(go (println
     (&amp;lt;! (let [c (async/chan 1 (stateful-counter))]

           (async/onto-chan! c (range 5))

           (async/into []
                       (async/take 10 c))))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;   [5 5 5 5 5 5 5 5 5 5]&lt;/p&gt;
&lt;p&gt;If I don't use (async/take 10 _) there seems to be an infinite loop. The expected outcome would be [5].&lt;/p&gt;
&lt;p&gt;I have also tried using net.cgrand.xforms/count from &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/cgrand/xforms&quot;&gt;the xforms library&lt;/a&gt; and get the same unexpected result:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(go (println
     (&amp;lt;! (let [c (async/chan 1 xforms/count)]

           (async/onto-chan! c (range 5))

           (async/into []
                       (async/take 10 c))))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I was assuming that I made an implementation error in stateful-counter that caused the loop. But am confused that xforms/count produces the same (unexpected) result.&lt;/p&gt;
&lt;p&gt;This is a ClojureScript snippet, but I have been able to produce the same unexpected result with Clojure.&lt;/p&gt;
&lt;p&gt;Can somebody help me understand why applying the stateful transducer on a core.async channel results in an infinite sequence?&lt;/p&gt;
&lt;p&gt;EDIT: Same unexpected behavior seen with Clojure.&lt;/p&gt;
</description>
<category>Transducers</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9529/infinite-loop-with-stateful-transducer-core-async-channel</guid>
<pubDate>Sat, 08 Aug 2020 13:03:55 +0000</pubDate>
</item>
<item>
<title>how to consume the chan return by go block in cljs</title>
<link>https://ask.clojure.org/index.php/9486/how-to-consume-the-chan-return-by-go-block-in-cljs</link>
<description>&lt;p&gt;I follow &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojurescript.org/guides/promise-interop&quot;&gt;this article&lt;/a&gt; to play with cljs, but wonder how to wait go block in the top level, the problem arise when eval go block in REPL, which return &lt;code&gt;#object[cljs.core.async.impl.channels.ManyToManyChannel]&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;After compiled to file, and exec via &lt;code&gt;node target/main.js&lt;/code&gt;, it seems top level go block get blocked, waiting for its execution.&lt;/p&gt;
&lt;p&gt;I suspect the difference between those two execution ways is related to how go is implement, or how event loop. works, but can't be sure.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(go ;; how to wait this ??
  (let [browser (&amp;lt;! (u/new-browser))
        page (&amp;lt;p! (.newPage browser))]
    (try
      (&amp;lt;p! (.goto page &quot;https://clojure.org&quot;))
      (&amp;lt;p! (.screenshot page #js{:path &quot;./target/screenshot.png&quot;}))
      (catch js/Error err
        (println (ex-cause err)))
      (finally (&amp;lt;p! (.close browser))))))  ;; eval here return ManyToManyChannel

(defn new-browser [&amp;amp; {:as opts}]
  (let [pp-opts (-&amp;gt; opts
        (update :user-data-dir #(or % (str home-dir &quot;/pp&quot;)))
        (clj-&amp;gt;js :keyword-fn csk/-&amp;gt;camelCase))]
    (go (&amp;lt;p! (.launch puppeteer pp-opts)))))
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>ClojureScript</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9486/how-to-consume-the-chan-return-by-go-block-in-cljs</guid>
<pubDate>Tue, 21 Jul 2020 00:19:03 +0000</pubDate>
</item>
<item>
<title>A lazy sequence backed by a channel</title>
<link>https://ask.clojure.org/index.php/9351/a-lazy-sequence-backed-by-a-channel</link>
<description>&lt;p&gt;I'm trying to convert a channel into a seq. This is what I'm using currently:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn chan-&amp;gt;seq [ch]
  (when [v (&amp;lt;!! ch)]
    (lazy-seq (cons v (chan-&amp;gt;seq ch)))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It works but it uses a blocking read, so the current thread will be blocked until the channel is closed, and you can't have many calls of this function active at the same time. Is it possible to have a non-blocking version of this function?&lt;/p&gt;
&lt;p&gt;This example demonstrates this issue:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(doseq [_ (range 20)]
  (go (chan-&amp;gt;seq (chan 10))))

(go (println :OK))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The last statement won't print anything since &lt;code&gt;chan-&amp;gt;seq&lt;/code&gt; calls are squatting on all the threads in the threadpool. It works if 20 is changed to 5 (in a new repl session).&lt;/p&gt;
</description>
<category>Clojure</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9351/a-lazy-sequence-backed-by-a-channel</guid>
<pubDate>Wed, 03 Jun 2020 13:22:25 +0000</pubDate>
</item>
<item>
<title>Add additional arity to the ex-handler [core.async]</title>
<link>https://ask.clojure.org/index.php/9257/add-additional-arity-to-the-ex-handler-core-async</link>
<description>&lt;p&gt;I find it limiting that &lt;code&gt;ex-handler&lt;/code&gt; for &lt;code&gt;async/pipeline&lt;/code&gt;(&lt;code&gt;-blocking&lt;/code&gt;) accepts an exception only, but not the value for which that exception occurred. Another arity &lt;code&gt;(fn [ex v])&lt;/code&gt; would be useful for use cases such as retrying, better error reporting etc. I guess it could be supported in a backward-compatible way via reflection arity checking (e.g. &lt;a rel=&quot;nofollow&quot; href=&quot;https://stackoverflow.com/a/1813967&quot;&gt;https://stackoverflow.com/a/1813967&lt;/a&gt;).&lt;/p&gt;
</description>
<category>core.async</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9257/add-additional-arity-to-the-ex-handler-core-async</guid>
<pubDate>Mon, 20 Apr 2020 22:53:25 +0000</pubDate>
</item>
<item>
<title>Is this the correct way to use go-block?</title>
<link>https://ask.clojure.org/index.php/9209/is-this-the-correct-way-to-use-go-block</link>
<description>&lt;p&gt;In general, we want to avoiding blocking IO and make it parking. Let us say we make saving to DB nonblocking by a &lt;code&gt;go&lt;/code&gt; block.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn save-user-info [user]
   (go (...)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now if I want to use this function, I want to call it in a &lt;code&gt;go&lt;/code&gt; block since I do not want blocking.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn register-user [user other-info]
  (go (validate-user ...)
      ...
      (&amp;lt;! (save-user-info user))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The function &lt;code&gt;save-user&lt;/code&gt; and &lt;code&gt;register-user&lt;/code&gt; can be used in many places and then we need another &lt;code&gt;go&lt;/code&gt; block.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn send-welcome-email [user params]
    (go ...
        (&amp;lt;! (register-user user))
        ...))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And again &lt;code&gt;send-welcome-email&lt;/code&gt; can be used in other functions (say &lt;code&gt;last-fn&lt;/code&gt;) and we then need another &lt;code&gt;go&lt;/code&gt; block. So we have created a chain of functions using &lt;code&gt;go&lt;/code&gt; since the &quot;origin&quot; one does.&lt;/p&gt;
&lt;p&gt;Then if we call &lt;code&gt;last-fn&lt;/code&gt;, we have to create several &lt;code&gt;go&lt;/code&gt; blocks one-by-one.&lt;/p&gt;
&lt;p&gt;My question is: is this the acceptable/correct way to use &lt;code&gt;go&lt;/code&gt;? Or my question is wrong.&lt;/p&gt;
&lt;p&gt; I am worried because it seems in order to execute one function, we need to create several &lt;code&gt;go&lt;/code&gt; (and then channels) and GC them. it is not elegant some how. I do not understand &lt;code&gt;core.async &lt;/code&gt; quite in deep but believe &lt;code&gt;go&lt;/code&gt; is to make IO non-blocking. On the other side, if we think &lt;code&gt;go&lt;/code&gt; as a state machine, then it seems we shall avoid IO in &lt;code&gt;go&lt;/code&gt; and make things inside side-effect free. &lt;/p&gt;
</description>
<category>IO</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9209/is-this-the-correct-way-to-use-go-block</guid>
<pubDate>Thu, 02 Apr 2020 09:52:47 +0000</pubDate>
</item>
<item>
<title>core.async thread pool vs willy-nilly Java threads?</title>
<link>https://ask.clojure.org/index.php/9196/core-async-thread-pool-vs-willy-nilly-java-threads</link>
<description>&lt;p&gt;core.async has a thread pool, limited by Java system property &quot;clojure.core.async.pool-size&quot;.  But the following little REPL interaction (briefly) creates thousands of Java threads.  &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(require '[clojure.core.async :as a])
(def p (a/promise-chan))
(def a (atom 0))
(def b (atom 0))
(def cc (into [] (repeatedly 10000 (fn []
                                     (a/go
                                       (swap! a inc)
                                       (a/&amp;lt;! p)
                                       (swap! b inc))))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Immediately upon the &lt;code&gt;(def cc...)&lt;/code&gt;, I noticed, among other things, that jstack (in another terminal) showed a brief increase of thousands of Java threads.&lt;/p&gt;
&lt;p&gt;Question: When is the fixed-size thread pool used, vs creating a Java thread for every &quot;go&quot;?&lt;/p&gt;
</description>
<category>Clojure</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9196/core-async-thread-pool-vs-willy-nilly-java-threads</guid>
<pubDate>Sun, 29 Mar 2020 16:51:04 +0000</pubDate>
</item>
</channel>
</rss>