Re: perform action after events stop for some period

2014-12-06 Thread Juan Martín
Hey Brian, Yes you are absolutely right, my bad, futures *do* run on a thread pool since they use the *clojure.lang.Agent/soloExecutor* created here

Re: perform action after events stop for some period

2014-12-06 Thread Brian Craft
I'm confused on the thread issue. Don't futures run from the agent thread pool? So they don't really create a thread? Also found climatecorp's claypoole library, which has a 'future' call that works with a thread pool, further confusing me. Not sure how that's different from the agent thread po

Re: perform action after events stop for some period

2014-12-06 Thread juan.facorro
Hi Brian, I had the same requirement while building an application, it included a GUI and I wanted to perform some actions only after the user was done editing some text. I also first implemented a solution using an atom, Thread/sleep and a future. It didn't seem right though, since I was creat

Re: perform action after events stop for some period

2014-12-04 Thread Dylan Butman
woops should actually be (go-loop [] (let [[v c] (alts! [(timeout period) events-ch])] (if (= c events-ch) (when v (recur)) (apply f args the timeout returns nil On Wednesday, December 3, 2014 3:47:35 PM UTC-5, Erik Price wrote: > > Thank you

Re: perform action after events stop for some period

2014-12-03 Thread Erik Price
Thank you for calling my attention to this possibility! e On Wed, Dec 3, 2014 at 2:49 PM, Dylan Butman wrote: > Erik that's pretty! But be careful about go-loops and closed channels. > This will recur infinitely if events-ch is closed (it will continuously > return nil) > > (defn invoke-after-u

Re: perform action after events stop for some period

2014-12-03 Thread Dylan Butman
Erik that's pretty! But be careful about go-loops and closed channels. This will recur infinitely if events-ch is closed (it will continuously return nil) (defn invoke-after-uninterrupted-delay ([period events-ch f] (invoke-after-uninterrupted-delay period events-ch f [])) ([period

Re: perform action after events stop for some period

2014-12-03 Thread Dylan Butman
Erik that's pretty! But be careful about go-loops and closed channels. This will recur infinitely if events-ch is closed (it will continuously return nil) (defn invoke-after-uninterrupted-delay ([period events-ch f] (invoke-after-uninterrupted-delay period events-ch f [])) ([period ev

Re: perform action after events stop for some period

2014-12-02 Thread Brian Craft
Cool. I haven't used core.async before, and am a bit reluctant to pull in another dependency just for this. But maybe it's the right solution. On Tuesday, December 2, 2014 10:03:54 AM UTC-8, Erik Price wrote: > > > > On Tue, Dec 2, 2014 at 12:22 PM, Brian Craft > wrote: > >> >> It does seem lik

Re: perform action after events stop for some period

2014-12-02 Thread Erik Price
On Tue, Dec 2, 2014 at 12:22 PM, Brian Craft wrote: > > It does seem like a single-thread solution would be better, not creating > so many futures. Polling seems pretty crude, but I don't see another way of > doing it with clojure abstractions. Maybe a pure java solution. > FWIW, the core.async-

Re: perform action after events stop for some period

2014-12-02 Thread Brian Craft
The nested send-off call doesn't happen on the same thread (it's in a future). Seems like that would be the same as if an unrelated thread called send-off while the outer send-off was running. It does seem like a single-thread solution would be better, not creating so many futures. Polling seem

Re: perform action after events stop for some period

2014-12-02 Thread Gary Verhaegen
In the general case, side effects within the swap! function are a bad idea because of the optimistic locking. In your first code snippet, if there is any contention on the atom (and maybe in your app you know there is none because it's only ever accesses by the same single thread), you run the risk

Re: perform action after events stop for some period

2014-12-01 Thread Erik Price
Coincidentally, we recently wrote code to do something very similar. The following function will invoke f after period milliseconds, unless a value is sent on events-ch, in which case the timeout is reset (and starts counting down again): (defn invoke-after-uninterrupted-delay ([period events-ch

Re: perform action after events stop for some period

2014-12-01 Thread Brian Craft
That version has the unfortunate behavior that (func) can be interrupted if (event) is called while it is running. Here's another version using an agent: (defn queue-with-delay2 [period func] (let [q (agent nil)] (fn [] (send-off q (fn [t] (when t

perform action after events stop for some period

2014-12-01 Thread Brian Craft
I have need to perform an action when a series of events is quiet for some period. That is, if one event arrives an action is queued to execute after some timeout. If a second event arrives the timeout is reset, and so-forth. The following code seems to work, however I'm wondering if calling 'fu