From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr 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 2BFF27FCEF for ; Tue, 21 Apr 2015 10:25:49 +0200 (CEST) Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of rlepigre@gmail.com) identity=pra; client-ip=209.85.212.178; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="rlepigre@gmail.com"; x-sender="rlepigre@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of rlepigre@gmail.com designates 209.85.212.178 as permitted sender) identity=mailfrom; client-ip=209.85.212.178; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="rlepigre@gmail.com"; x-sender="rlepigre@gmail.com"; 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-wi0-f178.google.com) identity=helo; client-ip=209.85.212.178; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="rlepigre@gmail.com"; x-sender="postmaster@mail-wi0-f178.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A0ACAQBGCDZVm7LUVdFbDgiDSFyDGLQyjkoJgU+GBIFCOBQBAQEBAQEBEQEBAQEBBgsLCRQuhCEBAQQSEQQLAQ0BLQsBAwwBBQUOCgICBQQdAgIPBSABBQEKGBMSCQeHdQMRBAEIqG0+MYs4hGGKNCcDCoUiAQEBBwIBGQEFDoETiRSBAoRRgyIvgRYFhSAKkAeGJYFbkgQ1gRVbgSshHYEUP20BgkMBAQE X-IPAS-Result: A0ACAQBGCDZVm7LUVdFbDgiDSFyDGLQyjkoJgU+GBIFCOBQBAQEBAQEBEQEBAQEBBgsLCRQuhCEBAQQSEQQLAQ0BLQsBAwwBBQUOCgICBQQdAgIPBSABBQEKGBMSCQeHdQMRBAEIqG0+MYs4hGGKNCcDCoUiAQEBBwIBGQEFDoETiRSBAoRRgyIvgRYFhSAKkAeGJYFbkgQ1gRVbgSshHYEUP20BgkMBAQE X-IronPort-AV: E=Sophos;i="5.11,614,1422918000"; d="scan'208";a="112238604" Received: from mail-wi0-f178.google.com ([209.85.212.178]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/RC4-SHA; 21 Apr 2015 10:25:48 +0200 Received: by widdi4 with SMTP id di4so12447267wid.0 for ; Tue, 21 Apr 2015 01:25:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:date:from:to:cc:subject:message-id:references:mime-version :content-type:content-disposition:content-transfer-encoding :in-reply-to:user-agent; bh=zONJfVQqAykuc5Z9v/xsFpfo9foP+UnMSvXy3gC8ckE=; b=NI9SnIVZMGkvNvNv6cVbQjUzRI0pTiXK6GyOQEOBL7ujr76R+XR51kLG7khtekkfjd nV8c7R4P3TzPZOhB0bOk52zbousNOXCjtk7cCMQ5tG0nTVI9mNZTV/fsYW1NqHyvh9io cTUOMwar2QtuOUUNwwwOj/CjtBrUih5vwMkzJnc9pEglGG8aCM+5/DgRJqxaCywiBD6x /Gt6IRTIuZk0GNnAjERwHEppHEwPjdyjUGjAhPi9qB9Au9LGRRIezGjiMU9jnY5vUoLI Q0evWDm95UF+zDQaAgZp6r86t1oJZ4xq51HNJ8McREl6cnVXOHtBLGYuTL+QE0s/yGRw DW1w== X-Received: by 10.180.88.8 with SMTP id bc8mr3511243wib.19.1429604748220; Tue, 21 Apr 2015 01:25:48 -0700 (PDT) Received: from localhost (nat-maths.univ-savoie.fr. [193.48.123.14]) by mx.google.com with ESMTPSA id ch2sm1955864wib.18.2015.04.21.01.25.47 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 21 Apr 2015 01:25:47 -0700 (PDT) Sender: Rodolphe Lepigre Date: Tue, 21 Apr 2015 10:25:45 +0200 From: Rodolphe Lepigre To: Benjamin Greenman Cc: OCaml mailing-list Message-ID: <20150421082545.GD23239@HPArchRod> References: <20150421001834.GB23239@HPArchRod> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) Subject: Re: [Caml-list] Timeout 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/