a few utils in CCFuture

This commit is contained in:
Simon Cruanes 2015-01-25 23:17:54 +01:00
parent feec3bf46f
commit bcdb1434e5
2 changed files with 30 additions and 8 deletions

View file

@ -348,7 +348,7 @@ let choose futures =
(* add handlers to all futures *) (* add handlers to all futures *)
List.iter List.iter
(fun fut -> (fun fut ->
on_finish fut on_finish fut
(fun res -> match res, !state with (fun res -> match res, !state with
| Done x, `Waiting -> state := `Done; set_done_ cell x | Done x, `Waiting -> state := `Done; set_done_ cell x
| Failed e, `Waiting -> state := `Done; set_fail_ cell e | Failed e, `Waiting -> state := `Done; set_fail_ cell e
@ -373,9 +373,17 @@ let slurp i_chan =
) )
in next () 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); (** Spawn a sub-process with the given command [cmd] (and possibly input);
returns a future containing (returncode, stdout, stderr) *) returns a future containing (returncode, stdout, stderr) *)
let spawn_process ?(stdin="") ~cmd () = let spawn_process ?(stdin="") cmd : subprocess_res t =
make make
(fun () -> (fun () ->
(* spawn subprocess *) (* spawn subprocess *)
@ -394,7 +402,11 @@ let spawn_process ?(stdin="") ~cmd () =
| Unix.WEXITED i -> i | Unix.WEXITED i -> i
| Unix.WSIGNALED i -> i | Unix.WSIGNALED i -> i
| Unix.WSTOPPED i -> i in | 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) let sleep time = make (fun () -> Thread.delay time)

View file

@ -94,13 +94,23 @@ val map : ('a -> 'b) -> 'a t -> 'b t
(** {2 Helpers} *) (** {2 Helpers} *)
val spawn_process : ?stdin:string -> cmd:string -> unit -> val read_chan : in_channel -> Bytes.t t
(int * string * string) t (** Read the whole channel *)
(** Spawn a sub-process with the given command [cmd] (and possibly input);
returns a future containing (returncode, stdout, stderr) *) 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 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} *) (** {2 Event timer} *)