On 20 February 2010 07:22, p.bernard <[email protected]> wrote:
> Two Question:
> 1. Is there a canonical method to limit eval execution time in the
> REPL.  i.e (A method to force a limitation on the number of seconds
> allowed to evaluate an clojure input resulting in a short circuited
> exit of the eval request.)
> 2. Is there a canonical method to limit the execution time of an
> arbitrary function call?

This is by no means canonical, but works for me:

(defmacro with-timeout
  "Attempt to get the result of body within ms milliseconds."
  [ms & body]
  `(let [f# (future ~...@body)]
     (.get f# ~ms java.util.concurrent.TimeUnit/MILLISECONDS)))

#_(example user=> (with-timeout 1 (reduce * (range 1 100001)))
           java.util.concurrent.TimeoutException (NO_SOURCE_FILE:0))
#_(example user=> (with-timeout 1 (reduce * (range 1 11)))
           3628800)


Regards,
Tim.

-- 
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

Reply via email to