From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id 83D2D7FCEF for ; Tue, 21 Apr 2015 02:54:27 +0200 (CEST) Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of blg59@cornell.edu) identity=pra; client-ip=209.85.215.45; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="blg59@cornell.edu"; x-sender="blg59@cornell.edu"; x-conformance=sidf_compatible Received-SPF: Neutral (mail3-smtp-sop.national.inria.fr: domain of blg59@cornell.edu does not assert whether or not 209.85.215.45 is permitted sender) identity=mailfrom; client-ip=209.85.215.45; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="blg59@cornell.edu"; x-sender="blg59@cornell.edu"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-la0-f45.google.com) identity=helo; client-ip=209.85.215.45; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="blg59@cornell.edu"; x-sender="postmaster@mail-la0-f45.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A0CRAQBenjVVmy3XVdFbFoNIXAWDErBXBoMTjiAzgU+GBAKBNgc6EgEBAQEBAQERAQEBAQEGCwsJFC6EIQEBAwESEQRHCwULCwQHGh0CAiISAQUBChIGEwgKCQeHdQMJCA2pPz4xiziPHQNOAYRdAQEBAQYBAQEBAR0KhgyFIYUABAeCaIFFBYUeCollhhqGJYFZkX8SI4EVggYhHYFtIjEBgkMBAQE X-IPAS-Result: A0CRAQBenjVVmy3XVdFbFoNIXAWDErBXBoMTjiAzgU+GBAKBNgc6EgEBAQEBAQERAQEBAQEGCwsJFC6EIQEBAwESEQRHCwULCwQHGh0CAiISAQUBChIGEwgKCQeHdQMJCA2pPz4xiziPHQNOAYRdAQEBAQYBAQEBAR0KhgyFIYUABAeCaIFFBYUeCollhhqGJYFZkX8SI4EVggYhHYFtIjEBgkMBAQE X-IronPort-AV: E=Sophos;i="5.11,612,1422918000"; d="scan'208";a="112198879" Received: from mail-la0-f45.google.com ([209.85.215.45]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/RC4-SHA; 21 Apr 2015 02:54:26 +0200 Received: by lagv1 with SMTP id v1so139756910lag.3 for ; Mon, 20 Apr 2015 17:54:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type; bh=mGY/goiTf526wEYHlKQY1GdITkAr/vPmk7+3s6053n4=; b=eZAEKs8RIxybLdudbFZ/HrIFN7fJM2gYkdqXjhzJMbSiDEOjNmyXmgy5IO+3NLNSYS psHvDiiBqmIz+e70raUFAJYOP7BxCQ9OUkVnABWXz3yIhwoyoiCyIXn7yoknwWTZ2I2y IU6mi7w8oAni/+XFSFGbGOSA/n5xSpJQxrPG8rWy+fUvUvXPA340WO0EZxYurF0lf8sf ahLL+JIFcw/ru3lapnQ5z2onmSpopsnblEw7cW0DtSAzihT/sIwkR+0poHZBgwXl3D47 AB/kyTQWSWWdcVHCwTS+J/Wh05laY4Wc+LvTKmEAE+5g9712VqqZjI4FSRiV4JqGItGq o5JQ== X-Gm-Message-State: ALoCoQmqbmmf0t/9/MbyDZs34YxSH6danbSYl+t/JtOMoO4DqXVgV87xni6UVWrMvD+lfDQJBlOa X-Received: by 10.152.29.42 with SMTP id g10mr6664235lah.58.1429577665348; Mon, 20 Apr 2015 17:54:25 -0700 (PDT) MIME-Version: 1.0 Received: by 10.112.230.38 with HTTP; Mon, 20 Apr 2015 17:54:05 -0700 (PDT) In-Reply-To: <20150421001834.GB23239@HPArchRod> References: <20150421001834.GB23239@HPArchRod> From: Benjamin Greenman Date: Mon, 20 Apr 2015 20:54:05 -0400 Message-ID: To: Rodolphe Lepigre Cc: OCaml mailing-list Content-Type: multipart/alternative; boundary=089e0158c796ec18500514317cc6 Subject: Re: [Caml-list] Timeout --089e0158c796ec18500514317cc6 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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 =3D Sys.Signal_handle (fun _ -> raise Timeout) let timeout (time : int) (f : 'a -> 'b) (arg : 'a) =3D let old_behavior =3D Sys.signal Sys.sigalrm sigalrm_handler in let reset_sigalrm () =3D ignore (Unix.alarm 0); Sys.set_signal Sys.sigalrm old_behavior in ignore (Unix.alarm time) ; let res =3D 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=C3=A9 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 > --089e0158c796ec18500514317cc6 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Here's a small function I use, taken from the book &qu= ot;Developing Applications with Objective Caml"

exception Timeout
let sigalrm_handler =3D Sys.Signal_handle (fun _ -> raise Timeout)
let timeout (time : int) (f : 'a -> 'b) (arg : 'a) =3D
   let old_behavior =3D Sys.signal Sys.sigalrm sigalrm_handler in
   let reset_sigalrm () =3D ignore (Unix.alarm 0); Sys.set_signal Sys.sigal=
rm old_behavior in
   ignore (Unix.alarm time) ;
   let res =3D f arg in reset_sigalrm () ; res

On Mon, Apr 20, 2015 at 8:1= 8 PM, Rodolphe Lepigre <rodolphe.lepigre@univ-savoie.fr&= gt; wrote:
I was wondering: is the= re 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

=C2=A0 exception Timeout
=C2=A0 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=C3=A9 de Savoie, FRANCE
http://l= ama.univ-savoie.fr/~lepigre/

--
Caml-list mailing list.=C2=A0 Subscription management and archives:
ht= tps://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

--089e0158c796ec18500514317cc6--