Re: core.async question - canceling thread
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 http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: core.async question - canceling thread
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 wrote: > 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 thread's locals as root set objects for GC, so native leaking would > only happen if the thread held native resources directly in locals, which > would only happen if it was executing a native method at the time of the > stop. I don't know if the JVM/JNI has a safeguard against native leaks from > threads being aborted while in native code, but I'd be mildly surprised if > it did. > > Clojure threads will potentially be holding locks if they are using > locking, dosync, swap!, or pretty much any of the concurrency primitives in > Clojure. That *might* include Var lookups; I'm not sure (*dynamic* var > lookups involve ThreadLocal, which might use locks under the hood). Many > Java objects use locks somewhere under the hood as well -- certainly > everything in j.u.concurrent is suspect in that regard (and therefore, > swap! and many other Clojure concurrency primitives). > > I'd be very leery of playing around with Thread.stop in any circumstance > more complicated than the thread's .run method is doing a pure math loop or > something similar. If it touches Java libraries (outside of > java.lang.String, java.math, and other value types) or uses Clojure > primitives (and how is it supposed to join its results back into the bigger > picture without them?) then it's dangerous. If it is a tight loop of math > stuff then you can check for the interrupted flag. > > My recommendation? Stay far, far away from Thread.stop (and .suspend) and > sprinkle Thread.sleep(1)s here and there in the math (maybe every certain > number of iterations -- a millisecond is still a LONG time compared to > primitive arithmetic ops). That should cause the thread to die with an > InterruptedException if .interrupt is called on it. If the thread does any > blocking I/O (or blocking core.async/j.u.concurrent stuff) with any > frequency it should also go tits up pretty quickly if .interrupted. > > > On Wed, Jan 22, 2014 at 4:31 PM, Mark Engelberg > wrote: > >> 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 > > wrote: >> >>> >>> >>> 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 executable. >>> >>> There's ProcessBuilder and Runtime.exec stuff, but it will have the JVM >>> and Clojure initialization overhead anyway. >>> >>> http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html >>> >>> http://www.tutorialspoint.com/java/lang/runtime_exec_envp.htm >>> >>> Shantanu >>> >>> -- >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "Clojure" group. >>> To post to this group, send email to clojure@googlegroups.com >>> Note that posts from new members are moderated - please be patient with >>> your first post. >>> To unsubscribe from this group, send email to >>> clojure+unsubscr...@googlegroups.com >>> For more options, visit this group at >>> http://groups.google.com/group/clojure?hl=en >>> --- >>> You received this message because you are subscribed to the Google >>> Groups "Clojure" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to clojure+unsubscr...@googlegroups.com. >>> For more options, visit https://groups.google.com/groups/opt_out. >>> >> >> -- >> -- >> You received this message because you are subscribed to the Google >> Groups "Clojure" group. >> To post to this group, send email to clojure@googlegroups.com >> Note that posts from new members are moderated - please be patient with >> your first post. >> To unsubscribe from this group, send email to >> clojure+unsubscr...@googlegroups.com >> For more options, visit this group at >> http://groups.google.com/group/clojure?hl=en >> --- >> You received this message because you are subscribed to the Google Groups >> "Clojure" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to clojure+unsubscr...@googlegroups.com. >> For more options, visit https://groups.google.com/groups/opt_out. >> > > -- > -- > You received this message because you are subscribed to the Google > G
Re: core.async question - canceling thread
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 thread's locals as root set objects for GC, so native leaking would only happen if the thread held native resources directly in locals, which would only happen if it was executing a native method at the time of the stop. I don't know if the JVM/JNI has a safeguard against native leaks from threads being aborted while in native code, but I'd be mildly surprised if it did. Clojure threads will potentially be holding locks if they are using locking, dosync, swap!, or pretty much any of the concurrency primitives in Clojure. That *might* include Var lookups; I'm not sure (*dynamic* var lookups involve ThreadLocal, which might use locks under the hood). Many Java objects use locks somewhere under the hood as well -- certainly everything in j.u.concurrent is suspect in that regard (and therefore, swap! and many other Clojure concurrency primitives). I'd be very leery of playing around with Thread.stop in any circumstance more complicated than the thread's .run method is doing a pure math loop or something similar. If it touches Java libraries (outside of java.lang.String, java.math, and other value types) or uses Clojure primitives (and how is it supposed to join its results back into the bigger picture without them?) then it's dangerous. If it is a tight loop of math stuff then you can check for the interrupted flag. My recommendation? Stay far, far away from Thread.stop (and .suspend) and sprinkle Thread.sleep(1)s here and there in the math (maybe every certain number of iterations -- a millisecond is still a LONG time compared to primitive arithmetic ops). That should cause the thread to die with an InterruptedException if .interrupt is called on it. If the thread does any blocking I/O (or blocking core.async/j.u.concurrent stuff) with any frequency it should also go tits up pretty quickly if .interrupted. On Wed, Jan 22, 2014 at 4:31 PM, Mark Engelberg wrote: > 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 > wrote: > >> >> >> 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 >>> executable. >>> >> >> There's ProcessBuilder and Runtime.exec stuff, but it will have the JVM >> and Clojure initialization overhead anyway. >> >> http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html >> >> http://www.tutorialspoint.com/java/lang/runtime_exec_envp.htm >> >> Shantanu >> >> -- >> -- >> You received this message because you are subscribed to the Google >> Groups "Clojure" group. >> To post to this group, send email to clojure@googlegroups.com >> Note that posts from new members are moderated - please be patient with >> your first post. >> To unsubscribe from this group, send email to >> clojure+unsubscr...@googlegroups.com >> For more options, visit this group at >> http://groups.google.com/group/clojure?hl=en >> --- >> You received this message because you are subscribed to the Google Groups >> "Clojure" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to clojure+unsubscr...@googlegroups.com. >> For more options, visit https://groups.google.com/groups/opt_out. >> > > -- > -- > You received this message because you are subscribed to the Google > Groups "Clojure" group. > To post to this group, send email to clojure@googlegroups.com > Note that posts from new members are moderated - please be patient with > your first post. > To unsubscribe from this group, send email to > clojure+unsubscr...@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/clojure?hl=en > --- > You received this message because you are subscribed to the Google Groups > "Clojure" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to clojure+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. > -- -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/grou
Re: core.async question - canceling thread
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 wrote: > > > 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 >> executable. >> > > There's ProcessBuilder and Runtime.exec stuff, but it will have the JVM > and Clojure initialization overhead anyway. > > http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html > > http://www.tutorialspoint.com/java/lang/runtime_exec_envp.htm > > Shantanu > > -- > -- > You received this message because you are subscribed to the Google > Groups "Clojure" group. > To post to this group, send email to clojure@googlegroups.com > Note that posts from new members are moderated - please be patient with > your first post. > To unsubscribe from this group, send email to > clojure+unsubscr...@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/clojure?hl=en > --- > You received this message because you are subscribed to the Google Groups > "Clojure" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to clojure+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. > -- -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: core.async question - canceling thread
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 > executable. > There's ProcessBuilder and Runtime.exec stuff, but it will have the JVM and Clojure initialization overhead anyway. http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html http://www.tutorialspoint.com/java/lang/runtime_exec_envp.htm Shantanu -- -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: core.async question - canceling thread
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 wrote: > 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 > wrote: > >> 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. >> >> One of the flagship examples for core.async is the example where you try >> two different engines to look something up, return the first one or >> timeout. Similarly, I want to try to solve a computational problem two >> different ways, and take the first solution or timeout. But of course, >> that only works if I can stop the computation from spinning CPU once I >> already have a solution (or timeout). >> >> Rewriting the underlying algorithms to constantly check for an interrupt >> is not an option. >> >> >> >> >> On Wed, Jan 22, 2014 at 11:31 AM, Praki Prakash >> wrote: >> >>> 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.com> wrote: >>> On Wed, Jan 22, 2014 at 2:51 AM, Jozef Wagner wrote: > 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. I would appreciate some further clarity on how future cancellations work, and if someone thinks it applies here, would like to see concretely how to do it. -- -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out. >>> >>> -- >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "Clojure" group. >>> To post to this group, send email to clojure@googlegroups.com >>> Note that posts from new members are moderated - please be patient with >>> your first post. >>> To unsubscribe from this group, send email to >>> clojure+unsubscr...@googlegroups.com >>> For more options, visit this group at >>> http://groups.google.com/group/clojure?hl=en >>> --- >>> You received this message because you are subscribed to the Google >>> Groups "Clojure" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to clojure+unsubscr...@googlegroups.com. >>> For more options, visit https://groups.google.com/groups/opt_out. >>> >> >> -- >> -- >> You received this message because you are subscribed to the Google >> Groups "Clojure" group. >> To post to this group, send email to clojure@googlegroups.com >> Note that posts from new members are moderated - please be patient with >> your first post. >> To unsubscribe from this group, send email to >> clojure+unsubscr...@googlegroups.com >> For more options, visit this group at >> http://groups.google.com/group/clojure?hl=en >> --- >> You received this message because you are subscribed to the Google Groups >> "Clojure" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to clojure+unsubscr...@googlegroups.com. >> For more options, visit https://groups.google.com/groups/opt_out. >> > > -- > -- > You received this message because you are subscribed to the Google > Groups "Clojure" group. > To post to this group, send email to clojure@googlegroups.com > Note that posts from new members are moderated - please be patient with > your first post. > To unsubscribe from this group, send email to > clojure+unsubscr...@googlegroups.co
Re: core.async question - canceling thread
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 wrote: > 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. > > One of the flagship examples for core.async is the example where you try > two different engines to look something up, return the first one or > timeout. Similarly, I want to try to solve a computational problem two > different ways, and take the first solution or timeout. But of course, > that only works if I can stop the computation from spinning CPU once I > already have a solution (or timeout). > > Rewriting the underlying algorithms to constantly check for an interrupt > is not an option. > > > > > On Wed, Jan 22, 2014 at 11:31 AM, Praki Prakash > wrote: > >> 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.com> wrote: >> >>> On Wed, Jan 22, 2014 at 2:51 AM, Jozef Wagner wrote: >>> 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. I would >>> appreciate some further clarity on how future cancellations work, and if >>> someone thinks it applies here, would like to see concretely how to do it. >>> >>> -- >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "Clojure" group. >>> To post to this group, send email to clojure@googlegroups.com >>> Note that posts from new members are moderated - please be patient with >>> your first post. >>> To unsubscribe from this group, send email to >>> clojure+unsubscr...@googlegroups.com >>> For more options, visit this group at >>> http://groups.google.com/group/clojure?hl=en >>> --- >>> You received this message because you are subscribed to the Google >>> Groups "Clojure" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to clojure+unsubscr...@googlegroups.com. >>> For more options, visit https://groups.google.com/groups/opt_out. >>> >> >> -- >> -- >> You received this message because you are subscribed to the Google >> Groups "Clojure" group. >> To post to this group, send email to clojure@googlegroups.com >> Note that posts from new members are moderated - please be patient with >> your first post. >> To unsubscribe from this group, send email to >> clojure+unsubscr...@googlegroups.com >> For more options, visit this group at >> http://groups.google.com/group/clojure?hl=en >> --- >> You received this message because you are subscribed to the Google Groups >> "Clojure" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to clojure+unsubscr...@googlegroups.com. >> For more options, visit https://groups.google.com/groups/opt_out. >> > > -- > -- > You received this message because you are subscribed to the Google > Groups "Clojure" group. > To post to this group, send email to clojure@googlegroups.com > Note that posts from new members are moderated - please be patient with > your first post. > To unsubscribe from this group, send email to > clojure+unsubscr...@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/clojure?hl=en > --- > You received this message because you are subscribed to the Google Groups > "Clojure" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to clojure+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. > -- -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: core.async question - canceling thread
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. One of the flagship examples for core.async is the example where you try two different engines to look something up, return the first one or timeout. Similarly, I want to try to solve a computational problem two different ways, and take the first solution or timeout. But of course, that only works if I can stop the computation from spinning CPU once I already have a solution (or timeout). Rewriting the underlying algorithms to constantly check for an interrupt is not an option. On Wed, Jan 22, 2014 at 11:31 AM, Praki Prakash wrote: > 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 > wrote: > >> On Wed, Jan 22, 2014 at 2:51 AM, Jozef Wagner wrote: >> >>> 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. I would >> appreciate some further clarity on how future cancellations work, and if >> someone thinks it applies here, would like to see concretely how to do it. >> >> -- >> -- >> You received this message because you are subscribed to the Google >> Groups "Clojure" group. >> To post to this group, send email to clojure@googlegroups.com >> Note that posts from new members are moderated - please be patient with >> your first post. >> To unsubscribe from this group, send email to >> clojure+unsubscr...@googlegroups.com >> For more options, visit this group at >> http://groups.google.com/group/clojure?hl=en >> --- >> You received this message because you are subscribed to the Google Groups >> "Clojure" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to clojure+unsubscr...@googlegroups.com. >> For more options, visit https://groups.google.com/groups/opt_out. >> > > -- > -- > You received this message because you are subscribed to the Google > Groups "Clojure" group. > To post to this group, send email to clojure@googlegroups.com > Note that posts from new members are moderated - please be patient with > your first post. > To unsubscribe from this group, send email to > clojure+unsubscr...@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/clojure?hl=en > --- > You received this message because you are subscribed to the Google Groups > "Clojure" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to clojure+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. > -- -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: core.async question - canceling thread
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 wrote: > On Wed, Jan 22, 2014 at 2:51 AM, Jozef Wagner wrote: > >> 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. I would > appreciate some further clarity on how future cancellations work, and if > someone thinks it applies here, would like to see concretely how to do it. > > -- > -- > You received this message because you are subscribed to the Google > Groups "Clojure" group. > To post to this group, send email to clojure@googlegroups.com > Note that posts from new members are moderated - please be patient with > your first post. > To unsubscribe from this group, send email to > clojure+unsubscr...@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/clojure?hl=en > --- > You received this message because you are subscribed to the Google Groups > "Clojure" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to clojure+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. > -- -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: core.async question - canceling thread
On Wed, Jan 22, 2014 at 2:51 AM, Jozef Wagner wrote: > 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. I would appreciate some further clarity on how future cancellations work, and if someone thinks it applies here, would like to see concretely how to do it. -- -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: core.async question - canceling thread
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 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 ( (if answer answer > (do (.stop th) nil > > But there are a couple bazillion sources that say you should never, ever > stop a thread. Is it perfectly safe in a situation like this where the > thread is running a pure function with no mutable state? > > Is there a better approach that is more integrated with core.async and > more safe? > -- -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: core.async question - canceling thread
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 (http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
core.async question - canceling thread
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))) (http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.