package lwt
Install
dune-project
Dependency
Authors
Maintainers
Sources
md5=16d58c8295b5273e58779db0ad1f967a
sha512=d5eafe2f5d2dc576b76db15b84caf69d673cb3d736dc3e40b239e6a42c6036658c78bd4b883e9f2b9f627c144be515b1824f1f2de9d79c05d5744c456fb63257
doc/index.html
Lwt
Introduction
Lwt is a concurrent programming library for OCaml. It provides a single data type: the promise, which is a value that will become resolved in the future. Creating a promise spawns a computation. When that computation is I/O, Lwt runs it in parallel with your OCaml code.
OCaml code, including creating and waiting on promises, is run in a single thread by default, so you don't have to worry about locking or preemption. You can detach code to be run in separate threads on an opt-in basis.
Here is a simplistic Lwt program which requests the Google front page, and fails if the request is not completed in five seconds:
let () =
let request =
let%lwt addresses = Lwt_unix.getaddrinfo "google.com" "80" [] in
let google = Lwt_unix.((List.hd addresses).ai_addr) in
Lwt_io.(with_connection google (fun (incoming, outgoing) ->
write outgoing "GET / HTTP/1.1\r\n";%lwt
write outgoing "Connection: close\r\n\r\n";%lwt
let%lwt response = read incoming in
Lwt.return (Some response)))
in
let timeout =
Lwt_unix.sleep 5.;%lwt
Lwt.return_none
in
match Lwt_main.run (Lwt.pick [request; timeout]) with
| Some response -> print_string response
| None -> prerr_endline "Request timed out"; exit 1
(* ocamlfind opt -package lwt.unix,lwt_ppx -linkpkg example.ml && ./a.out *)If you are not using the lwt_ppx syntax extension, you can use the let* binding opoerators from the Lwt.Syntax module instead.
let () =
let open Lwt.Syntax in
let request =
let* addresses = Lwt_unix.getaddrinfo "google.com" "80" [] in
let google = Lwt_unix.((List.hd addresses).ai_addr) in
Lwt_io.(with_connection google (fun (incoming, outgoing) ->
let* () = write outgoing "GET / HTTP/1.1\r\n" in
let* () = write outgoing "Connection: close\r\n\r\n" in
let* response = read incoming in
Lwt.return (Some response)))
in
let timeout =
let* () = Lwt_unix.sleep 5. in
Lwt.return_none
in
match Lwt_main.run (Lwt.pick [request; timeout]) with
| Some response -> print_string response
| None -> prerr_endline "Request timed out"; exit 1
(* ocamlfind opt -package lwt.unix,lwt_ppx -linkpkg example.ml && ./a.out *)In the program above, functions such as Lwt_io.write create promises. The let%lwt ... in construct (provided by lwt_ppx) or the let* ... in construct (provided by Lwt.Syntax) are used to wait for a promise to resolve. The code after in is scheduled to run after the code inside the let...in has resolved.
Lwt.pick races promises against each other, and behaves as the first one to complete.
Lwt_main.run forces the whole promise-computation network to be executed. All the visible OCaml code is run in a single thread, but Lwt internally uses a combination of worker threads and non-blocking file descriptors to resolve in parallel the promises that do I/O.
Tour
Lwt compiles to native code on Linux, macOS, Windows, and other systems. It's also routinely compiled to JavaScript for the front end and Node by js_of_ocaml.
In Lwt,
- The core library
Lwtprovides promises... - ...and a few pure-OCaml helpers, such as promise-friendly mutexes, condition variables, and mvars.
- There is a big Unix binding,
Lwt_unix, that binds almost every Unix system call. A higher-level moduleLwt_ioprovides nice I/O channels. Lwt_processis for subprocess handling.Lwt_preemptivespawns system threads.
Installing
- Use your system package manager to install a development libev package. It is often called
libev-devorlibev-devel. opam install conf-libev lwt
Additional Docs
- Manual (Online manual).
- Concurrent Programming with Lwt is a nice source of Lwt examples. They are translations of code from Real World OCaml, but are just as useful if you are not reading the book.
- Mirage Lwt tutorial.
- Example server written with Lwt.
API: Library lwt
This is the system-independent, pure-OCaml core of Lwt. To link with it, use (libraries lwt) in your dune file.
LwtAsynchronous programming with promises.Lwt_listList helpersLwt_streamData streamsLwt_resultExplicit error handlingLwt_mutexCooperative locks for mutual exclusionLwt_conditionConditionsLwt_mvarMailbox variablesLwt_switchLwt switchesLwt_poolExternal resource pools.
API: Library lwt.unix
This is the system call and I/O library. Despite its name, it is implemented on both Unix-like systems and Windows, although not all functions are available on Windows. To link with this library, use (libraries lwt.unix) in your dune file.
Lwt_unixCooperative system callsLwt_mainMain loop and event queueLwt_ioBuffered byte channelsLwt_processProcess managementLwt_bytesByte arraysLwt_preemptiveThis module allows to mix preemptive threads withLwtcooperative threads. It maintains an extensible pool of preemptive threads to which you can detach computations.Lwt_fmtFormat API for Lwt-powered IOsLwt_throttleRate limiters.Lwt_timeoutCancelable timeouts.Lwt_engineLwt unix main loop engineLwt_gcInteraction with the garbage collectorLwt_sysSystem informations.