Module Lwt_main
Main loop and event queue
val run : 'a Lwt.t -> 'aLwt_main.run pcalls the Lwt scheduler, performing I/O untilpresolves.Lwt_main.run preturns the value inpifpis fulfilled. Ifpis rejected with an exception instead,Lwt_main.run praises that exception.Every native and bytecode program that uses Lwt should call this function at its top level. It implements the Lwt main loop.
Example:
let main () = Lwt_io.write_line Lwt_io.stdout "hello world" let () = Lwt_main.run (main ())Lwt_main.runis not available when targeting JavaScript, because the environment (such as Node.js or the browser's script engine) implements the I/O loop.On Unix, calling
Lwt_main.runinstalls aSIGCHLDhandler, which is needed for the implementations ofLwt_unix.waitpidandLwt_unix.wait4. As a result, programs that callLwt_main.runand also use non-Lwt system calls need to handle those system calls failing withEINTR.Nested calls to
Lwt_main.runare not allowed. That is, do not callLwt_main.runin a callback triggered by a promise that is resolved by an outer invocation ofLwt_main.run. If your program makes such a call,Lwt_main.runwill raiseFailure. This should be considered a logic error (i.e., code making such a call is inherently broken).It is not safe to call
Lwt_main.runin a function registered withPervasives.at_exit, useLwt_main.at_exitinstead.
val yield : unit -> unit Lwt.tyield ()is a threads which suspends itself and then resumes as soon as possible and terminates.
module type Hooks = sig ... endHook sequences. Each module of this type is a set of hooks, to be run by Lwt at certain points during execution. See modules
Enter_iter_hooks,Leave_iter_hooks, andExit_hooks.
module Enter_iter_hooks : Hooks with type 'return_value kind = 'return_valueHooks, of type
unit -> unit, that are called before each iteration of the Lwt main loop.
module Leave_iter_hooks : Hooks with type 'return_value kind = 'return_valueHooks, of type
unit -> unit, that are called after each iteration of the Lwt main loop.
module Exit_hooks : Hooks with type 'return_value kind = 'return_value Lwt.tPromise-returning hooks, of type
unit -> unit Lwt.t, that are called at process exit. Exceptions raised by these hooks are ignored.
val enter_iter_hooks : (unit -> unit) Lwt_sequence.t- deprecated
Use module
Enter_iter_hooks.
val leave_iter_hooks : (unit -> unit) Lwt_sequence.t- deprecated
Use module
Leave_iter_hooks.
val exit_hooks : (unit -> unit Lwt.t) Lwt_sequence.t- deprecated
Use module
Exit_hooks.
val at_exit : (unit -> unit Lwt.t) -> unitLwt_main.at_exit hookis the same asignore (Lwt_main.Exit_hooks.add_first hook).