caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Timeout
@ 2015-04-21  0:18 Rodolphe Lepigre
  2015-04-21  0:54 ` Benjamin Greenman
  0 siblings, 1 reply; 3+ messages in thread
From: Rodolphe Lepigre @ 2015-04-21  0:18 UTC (permalink / raw)
  To: caml-list

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/

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Timeout
  2015-04-21  0:18 [Caml-list] Timeout Rodolphe Lepigre
@ 2015-04-21  0:54 ` Benjamin Greenman
  2015-04-21  8:25   ` Rodolphe Lepigre
  0 siblings, 1 reply; 3+ messages in thread
From: Benjamin Greenman @ 2015-04-21  0:54 UTC (permalink / raw)
  To: Rodolphe Lepigre; +Cc: OCaml mailing-list

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Timeout
  2015-04-21  0:54 ` Benjamin Greenman
@ 2015-04-21  8:25   ` Rodolphe Lepigre
  0 siblings, 0 replies; 3+ messages in thread
From: Rodolphe Lepigre @ 2015-04-21  8:25 UTC (permalink / raw)
  To: Benjamin Greenman; +Cc: OCaml mailing-list

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/

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2015-04-21  8:25 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-04-21  0:18 [Caml-list] Timeout Rodolphe Lepigre
2015-04-21  0:54 ` Benjamin Greenman
2015-04-21  8:25   ` Rodolphe Lepigre

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