caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Rodolphe Lepigre <rodolphe.lepigre@univ-savoie.fr>
To: Benjamin Greenman <blg59@cornell.edu>
Cc: OCaml mailing-list <caml-list@inria.fr>
Subject: Re: [Caml-list] Timeout
Date: Tue, 21 Apr 2015 10:25:45 +0200	[thread overview]
Message-ID: <20150421082545.GD23239@HPArchRod> (raw)
In-Reply-To: <CAAtAoRoxY0+Tfxeuejnqrd3Wseu4soudVQtFPmqbs05s2gBtGQ@mail.gmail.com>

On 20/04/2015 08:54, Benjamin Greenman wrote:
> 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

Great, thank you!

I only see one problem: when [Timeout] is raised, the signal handler is not
reset. This can be fix by doing something like:

  try let res = f arg in reset_sigalrm (); res
  with e -> (reset_sigalrm (); raise e)

This will have the advantage of transmitting other exceptions to the caller
as well.

Also, I guess [Unix.alarm time] should also be in the [try ... with ...].

Rodolphe

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

-- 
Rodolphe Lepigre
LAMA, Université de Savoie, FRANCE
http://lama.univ-savoie.fr/~lepigre/

      reply	other threads:[~2015-04-21  8:25 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
2015-04-21  8:25   ` Rodolphe Lepigre [this message]

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=20150421082545.GD23239@HPArchRod \
    --to=rodolphe.lepigre@univ-savoie.fr \
    --cc=blg59@cornell.edu \
    --cc=caml-list@inria.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;
as well as URLs for NNTP newsgroup(s).