package lwt_ppx
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=a214b07b89822bb7e0291edbba56e3fb41dbb48b2353e41a7c85c459f832d3eb
md5=47bdf4b429da94419941ebe4354f505f
Description
Published: 08 Mar 2018
README
Lwt
Lwt is OCaml's concurrent programming library. It provides a single data type: the promise, which is a value that will become determined 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) ->
let%lwt () = write outgoing "GET / HTTP/1.1\r\n" in
let%lwt () = write outgoing "Connection: close\r\n\r\n" in
let%lwt response = read incoming in
Lwt.return (Some response)))
in
let timeout =
let%lwt () = 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 -package lwt.ppx -linkpkg -o request example.ml
./request *)
In the program, functions such as Lwt_io.write
create promises. The let%lwt ... in
construct is used to wait for a promise to become determined; the code after in
is scheduled to run in a "callback." 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.
Overview
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 and BuckleScript.
In Lwt,
The core library
Lwt
provides 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_io
provides nice I/O channels.Lwt_process
is for subprocess handling.Lwt_preemptive
spawns system threads.The PPX syntax allows using all of the above without going crazy!
There are also some other helpers, such as
Lwt_react
for reactive programming. See the table of contents on the linked manual pages!
Installing
Use your system package manager to install a development libev package. It is often called
libev-dev
orlibev-devel
.opam install conf-libev lwt
Documentation
We are currently working on improving the Lwt documentation (drastically; we are rewriting the manual). In the meantime:
The current manual can be found here.
Mirage has a nicely-written Lwt tutorial.
An example of a simple server written in Lwt.
Concurrent Programming with Lwt is a nice source of Lwt examples. They are translations of code from the excellent Real World OCaml, but are just as useful if you are not reading the book.
Note: much of the current manual refers to 'a Lwt.t
as "lightweight threads" or just "threads." This will be fixed in the new manual. 'a Lwt.t
is a promise, and has nothing to do with system or preemptive threads.
Contact
Open an issue, visit Gitter chat, ask in #ocaml, on discuss.ocaml.org, or on Stack Overflow. Please do ask! Even apparently simple questions often end up educating other users, not to mention enlightening the maintainers!
Subscribe to the announcements issue to get news about Lwt releases. It is less noisy than watching the whole repository. Announcements are also made in /r/ocaml, on the OCaml mailing list, and on discuss.ocaml.org.
Contributing
We maintain easy starter issues. These are thoroughly explained and hyperlinked. We hope that this makes working on Lwt accessible even to relative OCaml beginners :)
CONTRIBUTING.md
contains tips for working on the code, such as how to check the code out, how review works, etc. There is also a high-level outline of the code base.The overall development plan can be found in the roadmap.
Ask us anything, whether it's about working on Lwt, or any question at all about it :)
The documentation always needs proofreading and fixes.
Despite a lot of progress, Lwt still needs more tests.
You are welcome to pick up any other issue, review a PR, add your opinion, etc.
Any feedback is welcome, including how to make contributing easier!
License
Lwt is released under the LGPL, with an OpenSSL linking exception. See COPYING
.
Related Libraries
alcotest A lightweight framework for unit testing
angstrom A library for building parsers with a focus on efficiency, concurrency, and reusability
cohttp A lightweight library for writing HTTP clients and servers
cstruct A library and syntax extension for interop with C-like structures
ezjsonm A library for easy interop with JSON
faraday A library for fast and memory-efficient serialization
logs A logging library with reporting decoupled from logging
lwt-parallel A library for distributed computing
Dependencies (5)
-
ppx_tools_versioned
>= "5.0.1"
-
ocaml-migrate-parsetree
< "2.0.0"
- lwt
-
jbuilder
>= "1.0+beta14"
-
ocaml
>= "4.02.0"
Dev Dependencies
None
Used by (47)
- anthill
-
azure-cosmos-db
< "0.2.3"
-
cohttp-lwt-jsoo
>= "4.1.1"
- css
-
devkit
>= "0.6"
-
docker_hub
< "0.2.0"
-
dream-encoding
>= "0.2.0"
-
dream-livereload
>= "0.2.0"
- dream-serve
-
earlybird
< "1.0.0"
-
elasticsearch-cli
>= "0.4"
-
eliom
>= "6.4.0" & < "6.13.1"
-
erssical
>= "1.1.0"
-
flowtype
>= "0.72.0"
-
gamepad
>= "0.2.0"
-
gdbprofiler
>= "0.3"
- hyper
-
i3ipc
>= "0.1.4"
-
jupyter
>= "2.3.0"
-
jupyter-kernel
= "0.4"
-
lambdapi
>= "2.4.0"
-
lru_cache
< "v0.16.0"
-
mpris
>= "0.2.0"
-
multipart-form-data
>= "0.2.0"
- mwt
- noise
-
obus
>= "1.2.0"
- opam-check-npm-deps
- openai
-
opentelemetry-client-cohttp-lwt
< "0.5"
-
oraft
>= "0.3.0"
- order-i3-xfce
- passage
-
plotkicadsch
>= "0.2.0"
-
ppx_defer
>= "0.4.0"
-
qfs
>= "0.7"
- quests
-
SZXX
< "2.0.0"
- scgi
- slack
-
slacko
>= "0.14.1"
- speed
-
spotify_ml
>= "push"
-
sqlexpr
>= "0.9.0"
- textrazor
-
usb
>= "1.3.1"
- ws-server
Conflicts
None