package async_unix

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type

Async.Process is for creating child processes of the current process, and communicating with children via their stdin, stdout, and stderr. Async.Process is the Async analog of Core.Unix.create_process and related functions.

type t
include sig ... end
val sexp_of_t : t -> Sexplib.Sexp.t
val pid : t -> Core.Pid.t

accessors

val stdin : t -> Writer.t
val stdout : t -> Reader.t
val stderr : t -> Reader.t
type env = Core.Unix.env
include sig ... end
val env_of_sexp : Sexplib.Sexp.t -> env
val sexp_of_env : env -> Sexplib.Sexp.t
type 'a create = ?buf_len:int -> ?env:env -> ?stdin:string -> ?working_dir:string -> prog:string -> args:string list -> unit -> 'a Async_unix.Import.Deferred.t

create ~prog ~args () uses fork and exec to create a child process that runs the executable prog with args as arguments. It creates pipes to communicate with the child process's stdin, stdout, and stderr.

Unlike exec, args should not include prog as the first argument.

If buf_len is supplied, it determines the size of the reader and writer buffers used to communicate with the child process's stdin, stdout, and stderr.

If stdin is supplied, then the writer to the child's stdin will have ~raise_when_consumer_leaves:false and ~buffer_age_limit:`Unlimited, which makes it more robust.

env specifies the environment of the child process.

If working_dir is supplied, then the child process will chdir() there before calling exec().

create returns Error if it is unable to create the child process. This can happen in any number of situations (unable to fork, unable to create the pipes, unable to cd to working_dir, etc.). create does not return error if exec fails; instead, it returns OK t, where wait t returns an Error.

val create : t Core.Or_error.t create
val create_exn : t create

wait t = Unix.waitpid (pid t)

module Output : sig ... end

collect_output_and_wait t closes stdin t and then begins collecting the output produced on t's stdout and stderr, continuing to collect output until t terminates and the pipes for stdout and stderr are closed. Usually when t terminates, the pipes are closed; however, t could fork other processes which survive after t terminates and in turn keep the pipes open -- wait will not become determined until both pipes are closed in all descendant processes.

val collect_output_and_wait : t -> Output.t Async_unix.Import.Deferred.t
type 'a run = ?accept_nonzero_exit:int list -> ?env:env -> ?stdin:string -> ?working_dir:string -> prog:string -> args:string list -> unit -> 'a Async_unix.Import.Deferred.t

run creates a process, feeds it stdin if provided, and waits for it to complete. If the process exits with an acceptable status, then run returns its stdout. If the process exits unacceptably, then run returns an error indicating what went wrong that includes stdout and stderr.

Acceptable statuses are zero, and any nonzero values specified in accept_nonzero_exit.

Some care is taken so that an error displays nicely as a sexp---in particular, if the child's output can already be parsed as a sexp, then it will display as a sexp (rather than a sexp embedded in a string). Also, if the output isn't a sexp, it will be split on newlines into a list of strings, so that it displays on multiple lines rather than a single giant line with embedded "\n"'s.

run_lines is like run but returns the lines of stdout as a string list, using String.split_lines.

run_expect_no_output is like run but expects the command to produce no output, and returns an error if the command does produce output.

val run : string Core.Or_error.t run
val run_exn : string run
val run_lines : string list Core.Or_error.t run
val run_lines_exn : string list run
val run_expect_no_output : unit Core.Or_error.t run
val run_expect_no_output_exn : unit run
type 'a collect = ?accept_nonzero_exit:int list -> t -> 'a Async_unix.Import.Deferred.t

collect_stdout_and_wait and collect_stdout_lines_and_wait are like run and run_lines but work from an existing process instead of creating a new one.

val collect_stdout_and_wait : string Core.Or_error.t collect
val collect_stdout_and_wait_exn : string collect
val collect_stdout_lines_and_wait : string list Core.Or_error.t collect
val collect_stdout_lines_and_wait_exn : string list collect
OCaml

Innovation. Community. Security.