Re: core.async question - canceling thread

2014-01-22 Thread Mark Engelberg
So far, this is the only way I've figured out that works: (defn try-fib [n] (let [ch (timeout 1000) th (Thread. #(!! ch (fib n))) _ (.start th) answer (!! ch)] (if answer answer (do (.stop th) nil But there are a couple bazillion sources that say you

Re: core.async question - canceling thread

2014-01-22 Thread Jozef Wagner
You can put the computation into a future, and cancel the future after the timeout. BTW is it idiomatic to write to the timeout channel? I thought one should use something like (alts!! [c (timeout 1000)]). JW On Wednesday, January 22, 2014 11:30:23 AM UTC+1, puzzler wrote: So far, this is

Re: core.async question - canceling thread

2014-01-22 Thread Mark Engelberg
On Wed, Jan 22, 2014 at 2:51 AM, Jozef Wagner jozef.wag...@gmail.comwrote: You can put the computation into a future, and cancel the future after the timeout. I experimented with that, but it didn't seem to work. I suspect that canceling a future doesn't really do what I think it should do.

Re: core.async question - canceling thread

2014-01-22 Thread Praki Prakash
What is the task doing? If it is in a tight loop, it must check explicitly whether the interrupt flag is set and abort. If it is waiting on some resource, it will receive InterruptedException. Regards, Praki Prakash On Wed, Jan 22, 2014 at 11:20 AM, Mark Engelberg mark.engelb...@gmail.comwrote:

Re: core.async question - canceling thread

2014-01-22 Thread Mark Engelberg
I think the fib example is a good one in the sense that you are dealing with an already function that takes a long time, and isn't written as a loop. But in general, I want to solve the problem for an arbitrary long-running computation, for example, a call into a library that you don't control.

Re: core.async question - canceling thread

2014-01-22 Thread Jozef Wagner
If you want to be able to control an arbitrary long-running function, a safe way is to run it in a separate process. That way you can safely terminate it anytime you want. Of course this opens up a lot of other issues :) On Wed, Jan 22, 2014 at 9:15 PM, Mark Engelberg

Re: core.async question - canceling thread

2014-01-22 Thread Mark Engelberg
Is there a convenient way within Clojure to launch a Clojure function or Java call in a separate process as opposed to a separate thread? Only way I know of is to literally shell out to the command prompt and launch a new executable. On Wed, Jan 22, 2014 at 12:19 PM, Jozef Wagner

Re: core.async question - canceling thread

2014-01-22 Thread Shantanu Kumar
On Thursday, 23 January 2014 02:37:43 UTC+5:30, puzzler wrote: Is there a convenient way within Clojure to launch a Clojure function or Java call in a separate process as opposed to a separate thread? Only way I know of is to literally shell out to the command prompt and launch a new

Re: core.async question - canceling thread

2014-01-22 Thread Mark Engelberg
So I guess this gets back to my earlier question: when is it safe to terminate a thread? I know that I often hit Ctrl-C in the REPL to terminate a long running function, and I've never really worried about it screwing things up. On Wed, Jan 22, 2014 at 1:29 PM, Shantanu Kumar

Re: core.async question - canceling thread

2014-01-22 Thread Cedric Greevey
It's not safe if the thread is holding any locks. It *may* also leak native resources if the thread is holding those, but native resources held by a heap-allocated Java object are supposed to be cleaned up by the finalizer if the object is GC'd, and I think Thread.stop properly removes that

Re: core.async question - canceling thread

2014-01-22 Thread Jozef Wagner
For processes, there is a https://github.com/Raynes/conch but I haven't used it yet so I don't know how mature it is. On Wed, Jan 22, 2014 at 10:45 PM, Cedric Greevey cgree...@gmail.com wrote: It's not safe if the thread is holding any locks. It *may* also leak native resources if the thread

Re: core.async question - canceling thread

2014-01-22 Thread Stephen Cagle
I took a stab at it in pure core.async, unfortunately, it does not work; I would be curious if anyone could explain why. (use '[clojure.core.async :only [timeout ! go !! alts!]]) (defn fib [n tmout] (let [res (go (if ( n 2) n (let [r1 (! (fib (- n 1)

core.async question - canceling thread

2014-01-21 Thread Mark Engelberg
Consider the really slow fib function: (defn fib [n] (if ( n 2) n (+ (fib (dec n)) (fib (- n 2) Now, let's say I want to compute the fib of some number n and timeout and return nil if it takes longer than a second. (defn try-fib [n] (let [ch (timeout 1000)] (go (! ch (fib n)))