Module Lwt_process

Contents

Instructions: Use this module in your project

In the IDE (CLion, Visual Studio Code, Xcode, etc.) you use for your DkSDK project:

  1. Add the following to your project's dependencies/CMakeLists.txt:

    Copy
    DkSDKProject_DeclareAvailable(lwt
        CONSTRAINT "= 5.6.1"
        FINDLIBS lwt lwt.unix)
    DkSDKProject_MakeAvailable(lwt)
  2. Add the Findlib::lwt library to any desired targets in src/*/CMakeLists.txt:

    Copy
    target_link_libraries(YourPackage_YourLibraryName
         # ... existing libraries, if any ...
         Findlib::lwt)
  3. Click your IDE's Build button

Not using DkSDK?

FIRST, do one or all of the following:

  1. Run:

    Copy
    opam install lwt.5.6.1
  2. Edit your dune-project and add:

    Copy
    (package
      (name YourExistingPackage)
      (depends
      ; ... existing dependenices ...
      (lwt (>= 5.6.1))))

    Then run:

    Copy
    dune build *.opam # if this fails, run: dune build
  3. Edit your <package>.opam file and add:

    Copy
    depends: [
      # ... existing dependencies ...
      "lwt" {>= "5.6.1"}
    ]

    Then run:

    Copy
    opam install . --deps-only

FINALLY, add the lwt.unix library to any desired (library)and/or (executable) targets in your **/dune files:

Copy
(library
  (name YourLibrary)
  ; ... existing library options ...
  (libraries
    ; ... existing libraries ...
    lwt.unix))

(executable
  (name YourExecutable)
  ; ... existing executable options ...
  (libraries
    ; ... existing libraries ...
    lwt.unix))

This module allows you to spawn processes and communicate with them.

type command`` = string * ``string array

A command. The first field is the name of the executable and the second is the list of arguments. For example:

Copy
("ls", [|"ls"; "-l"|])

Notes:

  • if the name is the empty string, then the first argument will be used. You should specify a name only if you do not want the executable to be searched in the PATH. On Windows the only way to enable automatic search in PATH is to pass an empty name.

  • it is possible to ``inline'' an argument, i.e. split it into multiple arguments. To do that prefix it with "\000". For example:

Copy
("", [|"echo"; "\000foo bar"|])

is the same as:

Copy
("", [|"echo"; "foo"; "bar"|])
valshell : ``string-> command

A command executed with the shell. (with "/bin/sh -c <cmd>" on Unix and "cmd.exe /c <cmd>" on Windows).

All the following functions take an optional argument timeout. If specified, after expiration, the process will be sent a Unix.sigkill signal and channels will be closed. When the channels are closed, any pending I/O operations on them (such as Lwt_io.read_chars) fail with exception Lwt_io.Channel_closed.

High-level functions

Redirections

typeredirection`` = ``[
| ``` Keep ``

(* Point to the same file as in the parent.

*)

| ``` Dev_null ``

(* Redirect to /dev/null (POSIX) or nul (Win32).

*)

| ``` Close ``

(* Close the file descriptor.

*)

| ``` FD_copy ``of Unix.file_descr

(* Redirect to the file pointed to by fd. fd remains open in the parent.

*)

| ``` FD_move ``of Unix.file_descr

(* Redirect to the file pointed to by fd. fd is then closed in the parent.

*)

]

File descriptor redirections. These are used with the ~stdin, ~stdout, and ~stderr arguments below to specify how the standard file descriptors should be redirected in the child process. All optional redirection arguments default to `Keep.

Executing

valexec : ``?timeout:float->``?env:``string array``->``?cwd:string-> ``?stdin:redirection -> ``?stdout:redirection -> ``?stderr:redirection -> command -> Unix.process_status Lwt.t

Executes the given command and returns its exit status.

Receiving

valpread : ``?timeout:float->``?env:``string array``->``?cwd:string-> ``?stdin:redirection -> ``?stderr:redirection -> command ->``stringLwt.t
valpread_chars : ``?timeout:float->``?env:``string array``->``?cwd:string-> ``?stdin:redirection -> ``?stderr:redirection -> command ->``charLwt_stream.t
valpread_line : ``?timeout:float->``?env:``string array``->``?cwd:string-> ``?stdin:redirection -> ``?stderr:redirection -> command ->``stringLwt.t
valpread_lines : ``?timeout:float->``?env:``string array``->``?cwd:string-> ``?stdin:redirection -> ``?stderr:redirection -> command ->``stringLwt_stream.t

Sending

valpwrite : ``?timeout:float->``?env:``string array``->``?cwd:string-> ``?stdout:redirection -> ``?stderr:redirection -> command ->``string->``unitLwt.t
valpwrite_chars : ``?timeout:float->``?env:``string array``->``?cwd:string-> ``?stdout:redirection -> ``?stderr:redirection -> command ->``charLwt_stream.t ->``unitLwt.t
valpwrite_line : ``?timeout:float->``?env:``string array``->``?cwd:string-> ``?stdout:redirection -> ``?stderr:redirection -> command ->``string->``unitLwt.t
valpwrite_lines : ``?timeout:float->``?env:``string array``->``?cwd:string-> ``?stdout:redirection -> ``?stderr:redirection -> command ->``stringLwt_stream.t ->``unitLwt.t

Mapping

valpmap : ``?timeout:float->``?env:``string array``->``?cwd:string-> ``?stderr:redirection -> command ->``string->``stringLwt.t
valpmap_chars : ``?timeout:float->``?env:``string array``->``?cwd:string-> ``?stderr:redirection -> command ->``charLwt_stream.t ->``charLwt_stream.t
valpmap_line : ``?timeout:float->``?env:``string array``->``?cwd:string-> ``?stderr:redirection -> command ->``string->``stringLwt.t
valpmap_lines : ``?timeout:float->``?env:``string array``->``?cwd:string-> ``?stderr:redirection -> command ->``stringLwt_stream.t ->``stringLwt_stream.t

Spawning processes

typestate`` =
| Running

(* The process is still running

*)

| Exited of Unix.process_status

(* The process has exited

*)

State of a sub-process

class process_none: ``?timeout:float->``?env:``string array``->``?cwd:string-> ``?stdin:redirection ->``?stdout:redirection -> ``?stderr:redirection -> command -> object...end
valopen_process_none : ``?timeout:float->``?env:``string array``->``?cwd:string-> ``?stdin:redirection -> ``?stdout:redirection -> ``?stderr:redirection -> command -> process_none
valwith_process_none : ``?timeout:float->``?env:``string array``->``?cwd:string-> ``?stdin:redirection -> ``?stdout:redirection -> ``?stderr:redirection -> command -> ``(process_none -> 'a Lwt.t)`` -> 'a Lwt.t
class process_in: ``?timeout:float->``?env:``string array``->``?cwd:string-> ``?stdin:redirection ->``?stderr:redirection -> command -> object...end
valopen_process_in : ``?timeout:float->``?env:``string array``->``?cwd:string-> ``?stdin:redirection -> ``?stderr:redirection -> command -> process_in
valwith_process_in : ``?timeout:float->``?env:``string array``->``?cwd:string-> ``?stdin:redirection -> ``?stderr:redirection -> command -> ``(process_in -> 'a Lwt.t)`` -> 'a Lwt.t
class process_out: ``?timeout:float->``?env:``string array``->``?cwd:string-> ``?stdout:redirection ->``?stderr:redirection -> command -> object...end
valopen_process_out : ``?timeout:float->``?env:``string array``->``?cwd:string-> ``?stdout:redirection -> ``?stderr:redirection -> command -> process_out
valwith_process_out : ``?timeout:float->``?env:``string array``->``?cwd:string-> ``?stdout:redirection -> ``?stderr:redirection -> command -> ``(process_out -> 'a Lwt.t)`` -> 'a Lwt.t
class process: ``?timeout:float->``?env:``string array``->``?cwd:string-> ``?stderr:redirection ->``command -> object...end
valopen_process : ``?timeout:float->``?env:``string array``->``?cwd:string-> ``?stderr:redirection -> command -> process
valwith_process : ``?timeout:float->``?env:``string array``->``?cwd:string-> ``?stderr:redirection -> command -> ``(process -> 'a Lwt.t)`` -> 'a Lwt.t
class process_full: ``?timeout:float->``?env:``string array``->``?cwd:string-> command -> object...end
valopen_process_full : ``?timeout:float->``?env:``string array``->``?cwd:string-> command -> process_full
valwith_process_full : ``?timeout:float->``?env:``string array``->``?cwd:string-> command -> ``(process_full -> 'a Lwt.t)`` -> 'a Lwt.t