Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure refs and add-watch

Tags:

clojure

Say I have a ref state which gets updated every 30 seconds and a fn I want to attach to it that may take longer to complete. Say at time 0 ref is updated and fn called at time 30 ref is again updated but fn is still running. Do I get two copies of the same function running or does it just skip and execute at time 60 assuming fn returns by then?

EDIT: I am trying to change the state of the ref. It is updated somewhere else I am just trying to use it as a trigger to do some control calculations.

like image 534
Hamza Yerlikaya Avatar asked Jan 30 '26 13:01

Hamza Yerlikaya


1 Answers

if you use a ref the two functions would run in parallel and compete for the right to produce the next state of the ref with the loser having to run again.

This is one of the differences between refs and agents. agents run sequentially because they have a queue of functions waiting to run on them.

like image 126
Arthur Ulfeldt Avatar answered Feb 03 '26 08:02

Arthur Ulfeldt