From bcdb1434e5c06391372a7bca8ebb5bc208098e81 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Sun, 25 Jan 2015 23:17:54 +0100 Subject: [PATCH] a few utils in CCFuture --- src/threads/CCFuture.ml | 18 +++++++++++++++--- src/threads/CCFuture.mli | 20 +++++++++++++++----- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/threads/CCFuture.ml b/src/threads/CCFuture.ml index 998daff7..8860cc5b 100644 --- a/src/threads/CCFuture.ml +++ b/src/threads/CCFuture.ml @@ -348,7 +348,7 @@ let choose futures = (* add handlers to all futures *) List.iter (fun fut -> - on_finish fut + on_finish fut (fun res -> match res, !state with | Done x, `Waiting -> state := `Done; set_done_ cell x | Failed e, `Waiting -> state := `Done; set_fail_ cell e @@ -373,9 +373,17 @@ let slurp i_chan = ) in next () +let read_chan ic = make1 slurp ic + +type subprocess_res = < + errcode : int; + stdout : Bytes.t; + stderr : Bytes.t; +> + (** Spawn a sub-process with the given command [cmd] (and possibly input); returns a future containing (returncode, stdout, stderr) *) -let spawn_process ?(stdin="") ~cmd () = +let spawn_process ?(stdin="") cmd : subprocess_res t = make (fun () -> (* spawn subprocess *) @@ -394,7 +402,11 @@ let spawn_process ?(stdin="") ~cmd () = | Unix.WEXITED i -> i | Unix.WSIGNALED i -> i | Unix.WSTOPPED i -> i in - (returncode, out', err') + object + method errcode = returncode + method stdout = out' + method stderr = err' + end ) let sleep time = make (fun () -> Thread.delay time) diff --git a/src/threads/CCFuture.mli b/src/threads/CCFuture.mli index fb7ac806..ff4691a5 100644 --- a/src/threads/CCFuture.mli +++ b/src/threads/CCFuture.mli @@ -94,13 +94,23 @@ val map : ('a -> 'b) -> 'a t -> 'b t (** {2 Helpers} *) -val spawn_process : ?stdin:string -> cmd:string -> unit -> - (int * string * string) t -(** Spawn a sub-process with the given command [cmd] (and possibly input); - returns a future containing (returncode, stdout, stderr) *) +val read_chan : in_channel -> Bytes.t t +(** Read the whole channel *) + +type subprocess_res = < + errcode : int; + stdout : Bytes.t; + stderr : Bytes.t; +> + +val spawn_process : ?stdin:string -> string -> subprocess_res t +(** Spawn a sub-process with the given command (and possibly input); + returns a future containing [(returncode, stdout, stderr)] *) val sleep : float -> unit t -(** Future that returns with success in the given amount of seconds *) +(** Future that returns with success in the given amount of seconds. Blocks + the thread! If you need to wait on many events, consider + using {!Timer} *) (** {2 Event timer} *)