Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the buffer size of a channel from clojure/core.async?

I would like to know how to get the size of a channel in clojure. I tried it with count, but it's not supported. Clojure documentation is usally good, but this time I coudn't find anything about it.

Example:

(def channel1 (chan 3))
(println(count channel1))
Should be 3 but  throws  "count not supported on this type: ManyToManyChannel"
like image 577
TruckerCat Avatar asked Dec 23 '22 22:12

TruckerCat


1 Answers

I found a solution.

(.buf (.buf ch)) ;; Get elements in buffer
;; => (:chan :on :elements)

(.count (.buf ch)) ;; Get number of elements in buffer
;; => 3

(.n (.buf ch)) ;; Get size of buffer
;; => 10

(.full? (.buf mychan)) ;; Is buffer full?
;; => false

Further reading here

like image 90
TruckerCat Avatar answered Feb 19 '23 12:02

TruckerCat