package lwt
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha256=699af62f5ae7d0c19d0b9cc453937e08bc04853e2ff0895e15e86eb3df32d23d
md5=1a72b5ae4245707c12656632a25fc18c
Description
A promise is a value that may become determined in the future.
Lwt provides typed, composable promises. Promises that are resolved by I/O are resolved by Lwt in parallel.
Meanwhile, OCaml code, including code creating and waiting on promises, runs in a single thread by default. This reduces the need for locks or other synchronization primitives. Code can be run in parallel on an opt-in basis.
Added to opam-repository:
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
Lwtprovides promises... - ...and a few pure-OCaml helpers, such as promise-friendly mutexes, condition variables, and mvars.
- There is a big Unix binding,
Lwt_unixthat binds almost every Unix system call. A higher-level moduleLwt_ioprovides nice I/O channels. Lwt_processis for subprocess handling.Lwt_preemptivespawns system threads.- The PPX syntax allows using all of the above without going crazy!
- There are also some other helpers, such as
Lwt_reactfor 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-devorlibev-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!
Release announcements are made in /r/ocaml, and on discuss.ocaml.org. Watching the repo for "Releases only" is also an option.
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.mdcontains 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!
Libraries to use with Lwt
- alcotest — unit testing
- angstrom — parser combinators
- cohttp — HTTP client and server
- cstruct — interop with C-like structures
- ezjsonm — JSON parsing and output
- faraday — serialization combinators
- logs — logging
- lwt-parallel — distributed computing
- mwt — preemptive (system) thread pools
- opium — web framework
Dependencies (8)
- seq
- result
- ocplib-endian
-
ocaml
>= "4.02.0" & < "4.12" -
mmap
>= "1.1.0" - dune-configurator
-
dune
>= "1.7.0" -
cppo
build & >= "1.1.0"
Dev Dependencies (2)
-
ocamlfind
with-dev-setup & >= "1.7.3-1" -
bisect_ppx
with-dev-setup & >= "1.3.0"
- 0install
- albatross
- alcotest-lwt
- alcotest-mirage
- ambient-context-lwt
-
amqp-client
>= "1.1.0" - amqp-client-lwt
- angstrom-lwt-unix
- anthill
- anycache-lwt
- archi-lwt
- arp
-
awa-mirage
< "0.2.0" - aws-lwt
- aws-s3-lwt
- awskit-lwt
- awskit-lwt-unix
- awskit-s3-lwt
- awskit-s3-lwt-unix
- awsm-lwt
- awso-lwt
-
azure-cosmos-db
< "0.4.0" - azure-cosmos-db-lwt
- balancer
- bimage-lwt
- bistro
- bitflyer
- brisk-reconciler
- builder
-
bun
>= "0.3.3" - cachet-lwt
- calculon
- caldav
- camltc
- canary
-
capnp-rpc-lwt
< "1.2.3" -
capnp-rpc-unix
< "1.2.3" -
caqti-lwt
< "2.0.1" -
carton-git
< "0.7.2" -
carton-lwt
< "1.0.0" -
catala-format
>= "0.2.0" - cf-lwt
- charrua-client
- charrua-unix
- ciao_lwt
- cmdtui-lambda-term
-
cohttp-lwt
< "6.0.0~beta2" -
cohttp-lwt-jsoo
!= "6.1.0" & < "6.2.1" - cohttp-lwt-unix
- cohttp-mirage
- comby
- comby-semantic
- conan-lwt
-
conduit-lwt
< "7.0.0" -
conduit-lwt-unix
< "7.0.0" - cowabloga
- crunch
- cstruct-lwt
- csv-lwt
-
ctypes
>= "0.15.0" & < "0.21.1" -
ctypes-foreign
>= "0.21.1" - curl_lwt
-
current
< "0.7.1" -
current_docker
< "0.7.1" -
current_examples
< "0.7.1" -
current_git
< "0.7.1" -
current_github
< "0.7.1" -
current_gitlab
< "0.7.1" -
current_ocluster
< "0.2" -
current_slack
< "0.7.1" -
current_web
< "0.7.1" - dap
-
data-encoding
< "0.1.1" - datakit-server
-
devkit
< "1.20261006" - distributed-lwt
- dkim-lwt-unix
- dkim-mirage
- dlm
- dns-certify
- dns-cli
-
dns-client
< "7.0.3" - dns-client-lwt
- dns-client-mirage
- dns-forward
- dns-forward-lwt-unix
- dns-lwt
- dns-mirage
- dns-resolver
- dns-server
- dns-stub
- dnssd
- docker_hub
- dream
- dream-httpaf
- dream-mirage
- dream-pure
- dropbox
-
dune
>= "3.17.2" - elasticsearch-cli
-
emoji
= "2.0.0" - ethernet
-
ez_api
< "3.0.0" - ezcurl-lwt
- ezjsonm-lwt
- ezresto
- ezresto-directory
- faraday-lwt
- faraday-lwt-unix
- fat-filesystem
- fiber-lwt
- fswatch_lwt
- fuseau-lwt
- gdbprofiler
- git
- git-paf
-
git-unix
>= "3.2.0" & < "3.15.0" - github
- github-hooks
-
github-unix
>= "4.4.0" - gitlab-unix
-
gluten-lwt
< "0.4.0" -
gluten-lwt-unix
< "0.4.0" -
gluten-mirage
< "0.4.0" - gmocoin
- graphql-lwt
- gremlin
- gufo
- h1
- h1-lwt-unix
-
h2-lwt
< "0.10.0" -
h2-lwt-unix
< "0.10.0" - h2-mirage
- happy-eyeballs-lwt
- happy-eyeballs-mirage
- hiredis
- hl_yaml
- http-lwt-client
-
http-multipart-formdata
>= "2.0.0" & < "3.0.0" - httpaf-lwt-unix
- httpun-lwt
- httpun-mirage
- httpun-ws-lwt
- hvsock
- i3ipc
- influxdb-lwt
-
inotify
>= "2.4" -
inquire
< "0.3.0" - interface-prime-lwt
- ip2location
- ip2locationio
- ip2whois
- ipv6-multicast-lwt
- irc-client-lwt
- irc-client-lwt-ssl
- irc-client-tls
-
irmin
< "2.7.1" - irmin-indexeddb
- irmin-watcher
- joolog
-
jose
< "0.9.0" - js_of_ocaml-lwt
- jsoo_broadcastchannel
- jsoo_storage
- jupyter
- jupyter-kernel
-
kafka
< "0.5" - kafka_lwt
- kappa-library
-
ke
>= "0.5" - kinetic-client
- kubecaml
- lambda-runtime
- lambda-term
- launchd
- learn-ocaml
- learn-ocaml-client
-
letsencrypt
< "2.0.0" - letsencrypt-app
-
letsencrypt-dns
< "2.0.0" -
links
< "0.9.5" - lru_cache
- lwt-binio
- lwt-canceler
- lwt-dllist
- lwt-exit
- lwt-parallel
- lwt-pipe
- lwt-pipeline
- lwt-watcher
- lwt-zmq
- lwt_camlp4
-
lwt_domain
< "0.3.0" -
lwt_eio
< "0.4" - lwt_glib
- lwt_log
- lwt_named_threads
-
lwt_ppx
< "5.8.0" - lwt_react
- lwt_ssl
-
mariadb
>= "1.2.0" - markdown_monolith
-
markup
= "0.7.6" - markup-lwt
- mdx
- mechaml
- metrics-influx
- metrics-lwt
- metrics-unix
- mindstorm-lwt
-
mirage
< "4.0.0" -
mirage-block
>= "2.0.1" - mirage-block-ccm
- mirage-block-combinators
- mirage-block-lwt
- mirage-block-ramdisk
- mirage-block-solo5
- mirage-block-unikraft
- mirage-block-xen
-
mirage-channel
>= "4.0.1" - mirage-channel-lwt
- mirage-clock-lwt
-
mirage-clock-unix
< "4.2.0" - mirage-console-lwt
-
mirage-crypto-rng
< "0.11.3" - mirage-crypto-rng-lwt
- mirage-crypto-rng-mirage
-
mirage-device
>= "2.0.0" -
mirage-flow
>= "3.0.0" - mirage-flow-combinators
- mirage-flow-lwt
- mirage-flow-unix
-
mirage-fs
>= "4.0.0" - mirage-fs-lwt
-
mirage-kv
>= "3.0.1" - mirage-kv-lwt
-
mirage-kv-unix
< "3.0.0" -
mirage-net
>= "4.0.0" - mirage-net-lwt
- mirage-net-macosx
- mirage-net-solo5
- mirage-net-unikraft
- mirage-net-unix
- mirage-net-xen
- mirage-profile
-
mirage-protocols
>= "7.0.0" - mirage-protocols-lwt
- mirage-runtime
- mirage-sleep
- mirage-solo5
-
mirage-stack
= "3.0.0" - mirage-stack-lwt
-
mirage-time
>= "3.0.0" - mirage-time-lwt
- mirage-time-unix
- mirage-types-lwt
- mirage-unikraft
- mirage-unix
- mirage-vnetif
- mirage-xen
-
moonpool-lwt
< "0.11" -
mrmime
>= "0.5.0" - multipart-form-data
-
multipart_form
>= "0.2.0" & < "0.4.0" -
multipart_form-lwt
< "0.6.0" - mwt
-
naboris
< "0.1.1" - named-pipe
- nbd
- nbd-tool
- nbd-unix
- nocrypto
- nottui-lwt
- notty-community
- nproc
- nsq
-
obuilder
< "0.4" - obus
-
ocluster
< "0.2" -
ocluster-api
< "0.2" - ocplib-resto
-
ocsigen-start
>= "4.1.0" & < "4.7.0" -
ocsigenserver
< "7.0.0" -
ocsipersist
< "3.1.2" -
ocsipersist-dbm
< "3.1.2" -
ocsipersist-lib
< "2.1.0" -
ocsipersist-pgsql
< "3.1.2" -
ocsipersist-sqlite
< "3.1.2" -
opam-compiler
< "0.2.0" -
opam-publish
= "0.3.5" | >= "3.0.0" - opencage
-
opium
< "0.19.0" - opium_kernel
- opomodoro
- order-i3-xfce
- ordma
- osc-lwt
-
oskel
>= "0.3.0" -
ounit-lwt
< "2.2.0" - ounit2-lwt
- owork
- paf
- paf-cohttp
-
passage
< "0.1.8" -
pcap-format
< "0.5.2" - pgx_lwt
- pgx_lwt_mirage
-
pgx_lwt_unix
< "2.0" - plotkicadsch
-
posix-getopt
>= "4.0.2" -
ppx_defer
>= "0.4.0" - ppx_deriving_rpc
- ppx_rapper_lwt
- prof_spacetime
-
prometheus
< "2.0" - prometheus-app
- prometheus-lwt
- promise_jsoo_lwt
- protocol-9p
- protocol-9p-unix
- proton
-
qcow
< "0.12.1" - qcow-tool
- qcow-types
- qfs
- quests
- quickterface
-
rawlink
< "2.1" - rawlink-lwt
- redis-lwt
- resource-pooling
- resp
-
resp-mirage
>= "0.10.0" -
resp-unix
>= "0.10.0" - resto
- resto-cohttp-client
- resto-cohttp-self-serving-client
- resto-cohttp-server
- resto-directory
- ringo-lwt
- river
- rpclib-js
- rpclib-lwt
- sanddb
- scgi
- sendmail-lwt
- sendmail-mirage
- serial
- session-cohttp-lwt
- session-cookie-lwt
- session-postgresql-lwt
- sessions
- shared-block-ring
- shared-memory-ring-lwt
- slacko
-
slipshow
< "0.12.0" -
smtml
>= "0.7.0" - speed
-
spin
< "0.6.0" -
stog
< "0.19.0" -
syndic
>= "1.4" & < "1.6.0" -
tar-unix
< "3.3.0" - tcpip
- telegraml
- timmy-lwt
-
tls
< "0.16.0" -
tls-lwt
< "0.17.5" - tls-mirage
- tube
- twirp_cohttp_lwt_unix
- uspf
- uspf-lwt
- uspf-mirage
- utop
- uwt
- vchan
- vchan-unix
- vchan-xen
- vercel
- vhd-format-lwt
- vmnet
-
vue-jsoo
< "0.3" - webauthn
- xen-evtchn
- xen-evtchn-unix
- xen-gnt
- xen-gnt-unix
-
xenstore
< "2.4.0" - xenstore-tool
- xenstore_transport
- xlsx2csv
- zarr-lwt
- zbar
- zmq-lwt
Conflicts (1)
-
ocaml-variants
= "4.02.1+BER"