package lwt
Install
Dune Dependency
Authors
Maintainers
Sources
md5=737039d29d45b2d2b35db6931c8d75c6
sha512=42e629920783428673b99c9d7a639237c9e6b35079b5d907bc67e7ea506acf9edadc48cec580bdcfd2410ed9412bf5e6bcc8b09de2fa7d35ce1490973d05ddd1
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.
Published: 17 Aug 2023
README
Lwt
Lwt is a concurrent programming library for OCaml. 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:
open Lwt.Syntax
let () =
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 -linkpkg example.ml && ./a.out *)
In the program, functions such as Lwt_io.write
create promises. The let* ... 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.
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 Discord chat, ask on discuss.ocaml.org, or on Stack Overflow.
Release announcements are made in /r/ocaml, and on discuss.ocaml.org. Watching the repo for "Releases only" is also an option.
Contributing
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.Ask us anything, whether it's about working on Lwt, or any question at all about it :)
The documentation always needs proofreading and fixes.
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 (5)
- ocplib-endian
-
ocaml
>= "4.08"
- dune-configurator
-
dune
>= "1.8.0"
-
cppo
build & >= "1.1.0"
Dev Dependencies (1)
-
ocamlfind
dev & >= "1.7.3-1"
-
0install
>= "2.15.1"
- aches-lwt
- activitypub
- albatross
- alcotest-lwt
- alcotest-mirage
- ambient-context-lwt
-
amqp-client
>= "0.9.0" & < "1.0.2" | >= "1.1.0"
-
amqp-client-lwt
>= "2.0.1"
-
angstrom-lwt-unix
>= "0.11.0"
- anthill
- anycache-lwt
-
arakoon
< "1.8.6" | >= "1.8.8"
- archi-lwt
-
arp
>= "2.3.1"
-
arp-mirage
>= "2.2.1"
- awa-lwt
- awa-mirage
- aws-lwt
-
aws-s3-lwt
< "4.4.0" | >= "4.8.1"
- awsm-lwt
- azure-cosmos-db
- baardskeerder
- balancer
-
bap
< "1.0.0"
-
bap-server
< "0.2.0"
- bastet_lwt
- bimage-lwt
-
biocaml
= "0.4.0"
-
bistro
>= "0.4.0"
- brozip
- builder
- builder-web
-
bun
>= "0.3.3"
- c3
- calculon
- caldav
-
camltc
= "0.9.5" | >= "0.9.7.0"
- canary
- capnp-rpc-lwt
-
capnp-rpc-unix
>= "0.9.0"
-
caqti-lwt
>= "0.11.0"
- caqti-mirage
- carton
- carton-git
- carton-lwt
- cf-lwt
- chamelon
- chamelon-unix
-
chamo
>= "3.0"
- channel
-
charrua-client
>= "1.3.0"
- charrua-client-lwt
- charrua-client-mirage
-
charrua-core
< "0.3"
-
charrua-unix
>= "0.3" & != "0.10"
- clz
- cmdtui-lambda-term
- coap
- coap-server-lwt
- coclobas
- cohttp-curl-lwt
- cohttp-lwt
- cohttp-lwt-jsoo
-
cohttp-lwt-unix
>= "1.1.1"
- cohttp-lwt-unix-nossl
- cohttp-lwt-unix-ssl
- cohttp-mirage
- cohttp-server-lwt-unix
- comby
- comby-semantic
- conan-lwt
- conduit-lwt
- conduit-lwt-unix
-
cowabloga
>= "0.2.2"
-
crunch
>= "2.0.0"
- cstruct-lwt
- csv-lwt
- csvprovider
-
ctypes
>= "0.15.0" & < "0.21.1"
-
ctypes-foreign
>= "0.21.1"
- current
- current-albatross-deployer
- current_docker
- current_examples
- current_git
- current_github
- current_gitlab
- current_ocluster
-
current_rpc
>= "0.4"
- current_slack
- current_web
- DkSDKFFIOCaml_Std
- dap
-
data-encoding
< "0.1.1"
- datakit
- datakit-bridge-github
- datakit-bridge-local-git
- datakit-ci
-
datakit-client
= "0.11.0"
- datakit-github
-
devkit
>= "1.2"
-
dht
< "0.2.0"
- distributed-lwt
-
dkim-bin
>= "0.6.0"
- dkim-mirage
- dlm
-
dns
>= "0.19.1" & < "0.20.1"
- dns-certify
-
dns-cli
>= "4.6.3"
-
dns-client
< "7.0.0"
- dns-client-lwt
- dns-client-mirage
-
dns-forward
>= "0.9.0"
- dns-forward-lwt-unix
- dns-lwt
- dns-mirage
- dns-resolver
- dns-server
- dns-stub
- dnssd
- docker_hub
-
docteur
>= "0.0.2"
- docteur-solo5
-
docteur-unix
>= "0.0.5"
- doi2bib
- dream
- dream-httpaf
- dream-pure
- dream-serve
- dropbox
-
dune-rpc-lwt
>= "3.7.0" & != "3.10.0"
- dune_watch
- earlybird
-
elasticsearch-cli
>= "0.4"
- equinoxe
- eris-lwt
- ethernet
-
ez_api
>= "1.2.0"
- ezcurl-lwt
- ezirmin
-
ezjs_min
< "0.2"
-
ezjsonm
>= "0.4.2" & < "0.5.0"
- ezjsonm-lwt
- ezresto
-
ezresto-directory
>= "0.5"
- faraday-lwt
-
faraday-lwt-unix
>= "0.6.0"
-
fat-filesystem
>= "0.12.0"
- fiber-lwt
-
flowtype
>= "0.72.0"
-
frenetic
< "2.0.0"
- fsevents-lwt
- fswatch_lwt
- fuseau-lwt
- gamepad
- gdb
-
gdbprofiler
>= "0.3"
-
git
!= "1.4.3" & != "1.7.2"
- git-cohttp
- git-cohttp-mirage
- git-cohttp-unix
-
git-mirage
>= "3.0.0"
- git-paf
-
git-unix
= "1.11.1" | >= "3.0.0"
- github
-
github-hooks
< "0.2.0" | >= "0.4.0"
-
github-unix
>= "4.4.0"
- gitlab-unix
- gitlab_pipeline_notifier
- gluten-lwt
-
gluten-lwt-unix
< "0.4.0"
-
gluten-mirage
< "0.4.0"
- graphql-lwt
- gremlin
- grpc-lwt
- guardian
- gufo
- h1
- h1-lwt-unix
- h2-lwt
-
h2-lwt-unix
< "0.10.0"
- h2-mirage
- happy-eyeballs-lwt
- happy-eyeballs-mirage
-
hardcaml
< "1.1.0"
-
hardcaml-examples
>= "0.3.0"
- hardcaml-framework
- hidapi-lwt
-
hiredis
!= "0.4"
- hl_yaml
- hockmd
-
horned_worm
< "0.3.1"
- http-lwt-client
- http-mirage-client
-
http-multipart-formdata
>= "2.0.0" & < "3.0.0"
- http2https
- httpaf-lwt-unix
- httpun-lwt
- httpun-mirage
- httpun-ws-lwt
-
hvsock
>= "1.0.2"
-
i3ipc
>= "0.1.4"
- imaplet-lwt
- influxdb-lwt
-
inotify
>= "2.4"
-
inquire
< "0.3.0"
- interface-prime-lwt
-
iocaml
< "0.4.6"
-
iocaml-kernel
>= "0.4.3" & < "0.4.6"
- iocamljs-kernel
- ip2location
- ip2locationio
- ip2whois
- ipv6-multicast-lwt
- irc-client-lwt
- irc-client-lwt-ssl
- irc-client-tls
-
irmin
< "0.9.6" | = "0.9.10" | >= "0.11.0"
- irmin-bench
- irmin-chunk
- irmin-cli
- irmin-client
- irmin-containers
-
irmin-fs
>= "2.3.0"
-
irmin-git
>= "2.3.0"
-
irmin-graphql
>= "2.3.0"
-
irmin-http
>= "2.3.0"
- irmin-indexeddb
- irmin-layers
-
irmin-mem
>= "2.3.0"
-
irmin-mirage-git
>= "2.3.0"
-
irmin-mirage-graphql
>= "2.3.0"
- irmin-pack
- irmin-server
-
irmin-test
>= "2.3.0"
-
irmin-unix
>= "2.3.0"
-
irmin-watcher
>= "0.3.0"
- jerboa
- jitsu
- joolog
-
jose
< "0.9.0"
-
js_of_ocaml
< "2.5"
-
js_of_ocaml-lwt
>= "3.5.0"
- jsoo_broadcastchannel
- jsoo_router
- jsoo_storage
-
jupyter
>= "2.3.0"
-
jupyter-kernel
>= "0.4"
-
kafka
>= "0.3" & < "0.5"
- kafka_lwt
-
kappa-agents
>= "4.1.3"
-
kappa-binaries
>= "4.1.3"
- kappa-library
-
ke
>= "0.5"
-
ketrew
>= "3.2.0"
-
kinetic-client
< "0.0.3" | >= "0.0.9"
- kubecaml
-
lablqml
< "0.6"
- lambda-runtime
-
lambda-term
>= "1.13"
- lambda_streams_lwt
- launchd
- ldp
-
learn-ocaml
>= "0.13.0"
-
learn-ocaml-client
>= "0.13.0"
-
ledgerwallet
>= "0.4.0"
- letsencrypt
- letsencrypt-app
- letsencrypt-dns
- letters
- libres3
-
links
!= "0.9"
- linol-lwt
- llama
-
lru_cache
< "v0.16.0"
-
lwt-binio
< "0.2.0"
- lwt-canceler
- lwt-dllist
- lwt-exit
-
lwt-parallel
>= "1.0.0"
- lwt-pipe
- lwt-pipeline
- lwt-watcher
-
lwt-zmq
< "1.0.0"
- lwt_camlp4
- lwt_domain
- lwt_eio
-
lwt_glib
>= "1.0.1"
-
lwt_log
>= "1.1.0"
- lwt_ppx
- lwt_ppx_let
-
lwt_react
>= "1.0.1"
-
lwt_ssl
>= "1.0.1"
-
mariadb
< "0.5.1"
-
markup
= "0.7.6"
- markup-lwt
- mdx
- mechaml
- mehari-lwt-unix
- mehari-mirage
- memtrace-mirage
- metrics-influx
- metrics-lwt
- metrics-mirage
- metrics-unix
- mimic
- mindstorm-lwt
-
mirage
>= "0.4.1" & != "0.6.1" & < "0.8.0" | >= "0.10.0" & < "2.7.0"
-
mirage-block
< "1.0.0" | >= "2.0.0"
- mirage-block-ccm
- mirage-block-combinators
- mirage-block-lwt
- mirage-block-partition
- mirage-block-ramdisk
- mirage-block-solo5
-
mirage-block-unix
< "2.3.0" | = "2.8.2" | >= "2.13.0"
- mirage-block-xen
-
mirage-bootvar-solo5
>= "0.2.0"
- mirage-bootvar-unix
-
mirage-bootvar-xen
>= "0.4.0"
-
mirage-channel
>= "4.0.0"
- mirage-channel-lwt
-
mirage-clock-freestanding
< "3.0.0"
- mirage-clock-lwt
-
mirage-clock-unix
>= "1.3.0" & < "3.0.0"
-
mirage-console
>= "3.0.0"
- mirage-console-lwt
-
mirage-console-solo5
>= "0.2.0"
-
mirage-console-unix
>= "2.2.1"
-
mirage-console-xen
>= "5.0.0"
- mirage-console-xen-backend
- mirage-console-xen-cli
- mirage-crypto-entropy
-
mirage-crypto-rng
>= "0.7.0" & < "0.11.0"
- mirage-crypto-rng-lwt
-
mirage-crypto-rng-mirage
>= "0.8.8"
-
mirage-device
>= "2.0.0"
-
mirage-dns
< "3.0.0"
- mirage-entropy
-
mirage-flow
>= "1.0.3" & < "1.2.0" | >= "2.0.0"
- mirage-flow-combinators
-
mirage-flow-lwt
< "1.3.0" | >= "1.5.0"
- mirage-flow-rawlink
-
mirage-flow-unix
>= "1.3.0"
-
mirage-fs
>= "3.0.0"
- mirage-fs-lwt
-
mirage-fs-unix
< "1.1.1" | >= "1.3.0"
- mirage-http
- mirage-http-unix
- mirage-http-xen
-
mirage-kv
>= "3.0.0"
- mirage-kv-lwt
- mirage-kv-unix
-
mirage-logs
!= "0.3.0"
-
mirage-nat
< "3.0.0"
-
mirage-net
>= "3.0.1"
- mirage-net-fd
- mirage-net-lwt
- mirage-net-macosx
- mirage-net-solo5
-
mirage-net-unix
>= "2.2.0"
- mirage-net-xen
-
mirage-os-shim
>= "3.0.0"
- mirage-profile
-
mirage-protocols
>= "4.0.0" & < "8.0.0"
- mirage-protocols-lwt
-
mirage-qubes
< "0.2" | >= "0.4"
- mirage-qubes-ipv4
-
mirage-random-stdlib
>= "0.1.0"
-
mirage-runtime
>= "3.7.0"
- mirage-solo5
-
mirage-stack
>= "2.0.0" & < "4.0.0"
- mirage-stack-lwt
-
mirage-time
>= "2.0.0"
- mirage-time-lwt
- mirage-time-unix
-
mirage-types-lwt
< "3.7.1"
-
mirage-unix
>= "3.0.0"
- mirage-vnetif
- mirage-vnetif-stack
-
mirage-www
>= "1.1.0"
- mirage-xen
- mirror
- monorobot
- moonpool-lwt
-
mqtt
>= "0.0.2"
-
mrmime
>= "0.5.0"
-
multipart-form-data
>= "0.2.0"
-
multipart_form
>= "0.2.0" & < "0.4.0"
-
multipart_form-cohttp-lwt
< "0.6.0"
- multipart_form-lwt
- mwt
- naboris
- nanomsg
-
nbd
= "2.1.1" | >= "4.0.3"
- nbd-tool
- nbd-unix
- netchannel
-
nocrypto
>= "0.5.4"
- nottui-lwt
- nproc
-
nsq
>= "0.4.0"
- obrowser
- obuilder
-
obus
>= "1.2.1"
-
ocaml-variants
>= "4.00.1+mirage-unix" & < "4.00.1+open-types"
- ocluster
- ocluster-api
- ocluster-worker
- ocplib-concur
- ocplib-resto
-
ocsigenserver
>= "2.10"
- ocsipersist
- ocsipersist-dbm
- ocsipersist-lib
- ocsipersist-pgsql
- ocsipersist-sqlite
- octez-distributed-lwt-internal
- octez-internal-libs
- octez-l2-libs
- octez-libs
- octez-proto-libs
- octez-protocol-compiler
- octez-proxy-server
- octez-rpc-process
- octez-shell-libs
- octez-smart-rollup-wasm-benchmark-lib
-
odoc
>= "2.0.0" & < "2.1.0"
- oframl
- ojquery
- ojs-base
- ojs_base
- omigrate
- oneffs
- opam-sync-github-prs
-
openflow
< "0.2.0"
- opentelemetry-client-cohttp-lwt
-
opentelemetry-cohttp-lwt
>= "0.4"
- opentelemetry-lwt
-
opium
>= "0.11.0" & != "0.16.0"
- opium-graphql
- opium_kernel
- opomodoro
- order-i3-xfce
-
ordma
>= "0.0.3"
-
oskel
>= "0.3.0"
-
ounit-lwt
< "2.2.0"
- ounit2-lwt
- owork
-
ox
< "1.1.0"
- ozulip
- paf
- paf-cohttp
- passage
-
pcap-format
>= "0.3.3" & < "0.5.0"
- petrol
- pgx_lwt
- pgx_lwt_mirage
-
pgx_lwt_unix
< "2.0"
-
piaf
< "0.2.0"
-
picos
>= "0.3.0" & < "0.5.0"
- picos_lwt
- picos_meta
-
plebeia
>= "2.0.0"
- plist-xml-lwt
-
plotkicadsch
>= "0.4.0"
-
ppx_defer
>= "0.4.0"
- ppx_deriving_rpc
- ppx_json_types
- ppx_netblob
- ppx_rapper_lwt
- ppx_sqlexpr
- proc-smaps
- prof_spacetime
- prometheus
- prometheus-app
- promise_jsoo_lwt
-
protocol-9p
>= "0.10.0"
- protocol-9p-unix
-
qcow
>= "0.8.1"
-
qcow-format
< "0.3"
- qcow-tool
-
qfs
= "0.5" | >= "0.7"
- quests
-
rawlink
>= "1.0" & < "2.1"
- rawlink-lwt
- rdf_json_ld
- rdf_lwt
- redis-lwt
- reparse-lwt
- reparse-lwt-unix
-
resource-pooling
>= "0.3.2"
- resp
-
resp-mirage
>= "0.10.0"
-
resp-unix
>= "0.10.0"
- resto
-
resto-cohttp-client
>= "0.4"
- resto-cohttp-self-serving-client
-
resto-cohttp-server
>= "0.4" & < "0.6" | >= "0.9"
-
resto-directory
>= "0.4"
- riak
- ringo-lwt
- river
- rock
-
rpc
>= "1.5.1" & < "7.1.0"
- rpclib-js
- rpclib-lwt
-
SZXX
< "4.0.0"
- sanddb
- scgi
- sendmail-lwt
- sendmail-mirage
- serial
- server-reason-react
- session-cohttp-lwt
- session-cookie-lwt
-
session-postgresql-lwt
>= "0.4.1"
- sessions
-
shared-block-ring
< "2.3.0" | >= "3.0.0"
-
shared-memory-ring
>= "1.2.0" & < "2.0.0"
- shared-memory-ring-lwt
- sherlodoc
-
sihl
< "0.2.0"
- slack
- slacko
- slipshow
-
smtml
>= "0.3.1"
-
socket-daemon
< "0.3.0"
- speed
-
spin
< "0.8.0"
- spoke
-
spotify-web-api
< "0.2.1"
-
sqlexpr
= "0.7.1" | >= "0.9.0"
- statsd-client
- stk
-
stog
>= "0.16.0"
- swapfs
- syguslib-utils
-
syndic
>= "1.4" & < "1.6.0"
-
tar-format
>= "0.4.1"
- tar-mirage
- tar-unix
-
tcpip
>= "3.1.1" & < "3.4.1" | >= "4.1.0"
- teash
- telegraml
- terminus
-
tezos-base
>= "13.0"
-
tezos-clic
>= "13.0"
-
tezos-crypto
>= "13.0"
- tezos-crypto-dal
-
tezos-error-monad
>= "8.0" & < "9.0" | >= "13.0"
-
tezos-lwt-result-stdlib
>= "9.0"
-
tezos-p2p
>= "11.0" & < "13.0"
-
tezos-protocol-compiler
>= "13.0"
-
tezos-protocol-environment
>= "13.0"
-
tezos-proxy
>= "17.1"
- tezos-proxy-server
- tezos-stdlib
-
tezos-stdlib-unix
>= "8.0" & < "9.0" | >= "13.0"
-
tezos-test-helpers
>= "12.0"
- tezos-wasmer
- tezos-webassembly-interpreter-extra
- tezt
- tftp
- themoviedb
- tidy_email
- timmy-lwt
-
tls
= "0.10.1" | >= "0.10.6" & < "0.16.0"
- tls-lwt
- tls-mirage
- transmission-rpc
-
tube
>= "4.3.0"
-
tuntap
>= "1.0.0" & < "1.7.0" | >= "2.0.0"
- twirp_cohttp_lwt_unix
- typerex-lldb
- u2f
- uring
- uspf
- uspf-lwt
- uspf-mirage
-
utop
>= "1.4.0"
-
uwt
>= "0.3.0"
-
vchan
>= "0.9.7" & < "2.0.0" | >= "2.0.3"
- vchan-unix
- vchan-xen
- vercel
-
vhd-format
>= "0.7.0" & < "0.8.0"
-
vhd-format-lwt
>= "0.12.0"
-
vhd-tool
< "0.12.0"
-
vmnet
>= "1.3.2"
-
vpnkit
>= "0.2.0"
-
vue-jsoo
< "0.3"
-
wayland
< "2.0"
- webauthn
-
websocket
< "2.3"
- xe-unikernel-upload
-
xen-api-client
< "0.9.14"
- xen-block-driver
-
xen-evtchn
< "1.0.6" | >= "2.0.0"
- xen-evtchn-unix
-
xen-gnt
>= "2.2.3"
-
xen-gnt-unix
>= "4.0.2"
-
xenctrl
< "0.9.29" | >= "0.9.32"
-
xenstore
>= "1.3.0"
-
xenstore_transport
>= "1.0.0"
- xentropyd
- xlsx2csv
- yocaml_git
-
yocaml_unix
< "2.0.0"
-
yurt
< "0.3"
- zarr-lwt
-
zmq-lwt
>= "5.2.1"
Conflicts
None