Another, perhaps cleaner method leverages more of Java's and Clojure's
concurrency tools.
(def res (atom nil))
(defn outer-loop [...]
(loop [x initial-value ...]
(reset! res x)
(recur (compute-new-x x ...) ...)))
...
(defn do-it [timeout ...]
(let [f (future (outer-loop ...))]
(try
(.get ^java.util.concurrent.Future f timeout
java.util.concurrent.TimeUnit/MILLISECONDS)
(catch Exception _
(future-cancel f)
@res))))
The do-it function returns in at most timeout milliseconds. It returns
the most recently generated value of res. If the timeout is reached,
the get method of Future throws an exception, which gets caught, and
the future-cancel method is called on f to interrupt the actual
computation. That will still need to be interruptible though or it
will run to completion anyway, using CPU to generate a maximally
refined result nobody will ever see. Or worse it may run forever.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en