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

View file

@ -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} *)