This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2024-04-28
Channels
- # announcements (2)
- # babashka (21)
- # beginners (24)
- # calva (9)
- # cider (7)
- # clj-kondo (12)
- # clojure (116)
- # clojure-europe (5)
- # clojure-korea (2)
- # clojure-norway (3)
- # clojure-poland (1)
- # clojure-spec (5)
- # clojurescript (12)
- # cursive (12)
- # datomic (8)
- # google-cloud (4)
- # honeysql (16)
- # java (18)
- # lsp (10)
- # missionary (14)
- # polylith (12)
- # re-frame (13)
- # releases (4)
- # shadow-cljs (10)
- # sql (10)
- # testify (2)
hi! I have a question about m/race
:
My understanding of the behavior of race
is: the second
task should execute successfully, returning 2, and the first sleep
task should be cancelled, so the only printed content should be :s 2
,
and :clean
should not appear.
but it looks like the second task is cancelled by m/race
(def cancel2 ((m/race (m/sleep 1000 1)
(fn [s f]
(s 2)
#(prn :clean)))
#(prn :s %) #(js/console.log :f %)))
;; print:
;; :s 2
;; :clean
Observing :clean
is an implementation detail. The cancelling function is supposed to become a no-op after completion, therefore m/race
considers it safe to call it on the race winner.
so even if the second task wins in the race, its canceller will still be called? did i understand it correctly?
According to the docstring “If any task succeeds, others are cancelled then race completes with this result.” I originally thought that the canceller of the winning task would definitely not be called.
The reason is because cancellation and completion may be concurrent, when it happens the task process must be able to resolve the race condition and handle the "completion then cancellation" scenario. It is therefore always safe for the parent process to cancel after completion, so it's allowed to do that if it's beneficial for the implementation.
Thank you so much for the detailed explanation! It might be best to add this explanation to the docstring or somewhere else? Otherwise, beginners like me might misunderstand the meaning of the m/race docstring.
This is expert-only material, as an application developer you're not supposed to write tasks manually
@U053XQP4S by writing tasks manually you mean writing tasks following the task spec without using missionary api (eg m/sp)?