From: Benjamin Greenman <blg59@cornell.edu>
To: Rodolphe Lepigre <rodolphe.lepigre@univ-savoie.fr>
Cc: OCaml mailing-list <caml-list@inria.fr>
Subject: Re: [Caml-list] Timeout
Date: Mon, 20 Apr 2015 20:54:05 -0400 [thread overview]
Message-ID: <CAAtAoRoxY0+Tfxeuejnqrd3Wseu4soudVQtFPmqbs05s2gBtGQ@mail.gmail.com> (raw)
In-Reply-To: <20150421001834.GB23239@HPArchRod>
[-- Attachment #1: Type: text/plain, Size: 1546 bytes --]
Here's a small function I use, taken from the book "Developing Applications
with Objective Caml"
http://caml.inria.fr/pub/docs/oreilly-book/html/book-ora168.html
exception Timeout
let sigalrm_handler = Sys.Signal_handle (fun _ -> raise Timeout)
let timeout (time : int) (f : 'a -> 'b) (arg : 'a) =
let old_behavior = Sys.signal Sys.sigalrm sigalrm_handler in
let reset_sigalrm () = ignore (Unix.alarm 0); Sys.set_signal
Sys.sigalrm old_behavior in
ignore (Unix.alarm time) ;
let res = f arg in reset_sigalrm () ; res
On Mon, Apr 20, 2015 at 8:18 PM, Rodolphe Lepigre <
rodolphe.lepigre@univ-savoie.fr> wrote:
> I was wondering: is there a standard way to stop a computation after, say,
> a given number of milliseconds (or seconds) in OCaml?
>
> For instance I would like to have a function
>
> exception Timeout
> val exec : int -> ('a -> 'b) -> 'a -> 'b
>
> such that [exec t f x] computes [f x] but raises [Timeout] in case the
> computation did not end before [t] milliseconds (or seconds).
>
> My guess would be that I need to use some Unix signals magic. Has anyone
> come up with a clean solution to this problem?
>
> Thanks!
>
> Rodolphe
> --
> Rodolphe Lepigre
> LAMA, Université de Savoie, FRANCE
> http://lama.univ-savoie.fr/~lepigre/
>
> --
> Caml-list mailing list. Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
[-- Attachment #2: Type: text/html, Size: 2451 bytes --]
next prev parent reply other threads:[~2015-04-21 0:54 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-04-21 0:18 Rodolphe Lepigre
2015-04-21 0:54 ` Benjamin Greenman [this message]
2015-04-21 8:25 ` Rodolphe Lepigre
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAAtAoRoxY0+Tfxeuejnqrd3Wseu4soudVQtFPmqbs05s2gBtGQ@mail.gmail.com \
--to=blg59@cornell.edu \
--cc=caml-list@inria.fr \
--cc=rodolphe.lepigre@univ-savoie.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox