package lwt
Install
Dune Dependency
Authors
Maintainers
Sources
md5=279024789a0ec84a9d97d98bad847f97
sha512=698875bd3bfcd5baa47eb48e412f442d289f9972421321541860ebe110b9af1949c3fbc253768495726ec547fe4ba25483cd97ff39bc668496fba95b2ed9edd8
doc/lwt.unix/Lwt_main/index.html
Module Lwt_main
Source
Main loop and event queue
This module controls the ``main-loop'' of Lwt.
Lwt_main.run p
calls the Lwt scheduler, performing I/O until p
resolves. Lwt_main.run p
returns the value in p
if p
is fulfilled. If p
is rejected with an exception instead, Lwt_main.run p
raises 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.run
is 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.run
installs a SIGCHLD
handler, which is needed for the implementations of Lwt_unix.waitpid
and Lwt_unix.wait4
. As a result, programs that call Lwt_main.run
and also use non-Lwt system calls need to handle those system calls failing with EINTR
.
Nested calls to Lwt_main.run
are not allowed. That is, do not call Lwt_main.run
in a callback triggered by a promise that is resolved by an outer invocation of Lwt_main.run
. If your program makes such a call, Lwt_main.run
will raise Failure
. 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.run
in a function registered with Stdlib.at_exit
, use Lwt_main.at_exit
instead.
yield ()
is a pending promise that is fulfilled after Lwt finishes calling all currently ready callbacks, i.e. it is fulfilled on the next “tick.”
Currently, paused promises are resolved more frequently than yielded promises. The difference is unintended but existing applications could depend on it. Unifying the two pools of promises into one in the future would eliminate possible discrepancies and simplify the code.
Causes promises created with Lwt.pause
and Lwt_main.yield
to remain forever pending.
yield
is now deprecated in favor of the more general Lwt.pause
. Once yield
is phased out, this function will be deprecated as well.
This is meant for use with Lwt_unix.fork
, as a way to “abandon” more promise chains that are pending in your process.
Hook 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
, and Exit_hooks
.
Hooks, of type unit -> unit
, that are called before each iteration of the Lwt main loop.
Hooks, of type unit -> unit
, that are called after each iteration of the Lwt main loop.
Promise-returning hooks, of type unit -> unit Lwt.t
, that are called at process exit. Exceptions raised by these hooks are ignored.