Fork me on GitHub
#core-async
<
2018-04-27
>
Garrett Hopper03:04:09

Is there a good way of making a particular function run every n seconds—regardless of how long it takes to execute the function? I know I can do (<! (timeout 1000)), execute the function, then loop, but then it would get slowly out of sync as the function takes time to execute.

tbaldridge13:04:50

I think this will work:

(let [t (timeout 1000)
   (run-function...)
   (<! t))

Garrett Hopper13:04:08

Thanks, @U064X3EF3 @U07TDTQNL, oh I hadn't thought about creating the timeout channel before using it. Would is still fall out of sync from the time it takes to create the timeout channel?

tbaldridge14:04:13

@U5JUDH2UE yes, and it will be out of sync because timeouts are only accurate to about 5-10ms. But really if you need better resolution than that, I don't know that core.async is the best tool

tbaldridge14:04:42

And by "resolution" mean that multiple calls to timeout within a . 5-10ms window all get the same timeout channel

lxsameer09:04:34

new threads ?

lxsameer09:04:39

or you can create a thread, schedule a new thread tobe created in 1000 and execute your function

lxsameer09:04:09

but in general i think it would be better to use a proper scheduler

leonoel09:04:50

is there a chance the ioc machinery will be eventually available as a separate library ?

Alex Miller (Clojure team)11:04:21

Rich has talked about the idea of moving a subset of it into core but I find it hard to imagine that bubbling to the top of the list any time soon

leonoel11:04:36

glad to hear there's interest to move in that direction anyways