package lwt

  1. Overview
  2. Docs
Promises and event-driven I/O

Install

dune-project
 Dependency

Authors

Maintainers

Sources

6.0.0-alpha00.tar.gz
md5=8a88c5d5fcb01d179bbb12e6b6888dca
sha512=bfecd3fa0a3461a3368bd7724e2fce7a751ba3899d590d5b08bc4257b058ca34ec25de1a3ead6585528afe77c6ca6db1c3e2d2421d6a773c845ef091472d2ed9

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: 24 Jul 2025

README

Lwt

version GitHub Actions status

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 module Lwt_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

  1. Use your system package manager to install a development libev package. It is often called libev-dev or libev-devel.
  2. 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 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

Dependencies (5)

  1. ocplib-endian
  2. dune-configurator
  3. cppo build & >= "1.1.0"
  4. ocaml >= "4.08"
  5. dune >= "2.7"

Dev Dependencies (3)

  1. bisect_ppx with-test
  2. odoc with-doc & >= "2.3.0"
  3. ocamlfind dev & >= "1.7.3-1"

  1. 0install >= "2.15.1"
  2. aches-lwt
  3. activitypub
  4. albatross
  5. alcotest-lwt
  6. alcotest-mirage
  7. ambient-context-lwt
  8. amqp-client >= "1.1.0"
  9. amqp-client-lwt
  10. angstrom-lwt-unix >= "0.11.0"
  11. anthill
  12. anycache-lwt
  13. archi-lwt
  14. arp
  15. awa-mirage
  16. aws-lwt
  17. aws-s3-lwt < "4.4.0" | >= "4.8.1"
  18. awsm-lwt
  19. azure-cosmos-db
  20. balancer
  21. bastet_lwt
  22. bistro
  23. brisk-reconciler
  24. brozip
  25. builder
  26. builder-web
  27. bun >= "0.3.3"
  28. cachet-lwt
  29. calculon
  30. caldav
  31. camltc
  32. canary
  33. capnp-rpc-lwt < "2.0"
  34. capnp-rpc-unix < "2.1"
  35. caqti-mirage
  36. carton < "1.0.0"
  37. carton-git
  38. carton-lwt
  39. catala-format >= "0.2.0"
  40. cf-lwt
  41. chamelon
  42. chamelon-unix
  43. chamo
  44. charrua-client
  45. charrua-unix
  46. chess_com_api
  47. clz
  48. cmdtui-lambda-term
  49. coap
  50. coap-server-lwt
  51. cohttp-curl-lwt
  52. cohttp-lwt
  53. cohttp-lwt-jsoo
  54. cohttp-lwt-unix
  55. cohttp-mirage
  56. cohttp-server-lwt-unix
  57. comby
  58. comby-semantic
  59. conan-lwt
  60. conduit-lwt
  61. conduit-lwt-unix
  62. cowabloga
  63. crunch
  64. cstruct-lwt
  65. csv-lwt
  66. ctypes >= "0.15.0" & < "0.21.1"
  67. ctypes-foreign >= "0.21.1"
  68. current
  69. current-albatross-deployer
  70. current_docker
  71. current_examples
  72. current_git
  73. current_github
  74. current_gitlab
  75. current_ocluster
  76. current_rpc
  77. current_slack
  78. current_web
  79. DkSDKFFIOCaml_Std
  80. dap
  81. data-encoding < "0.1.1"
  82. devkit >= "1.2"
  83. distributed-lwt
  84. dkim-bin < "0.8.0"
  85. dkim-lwt-unix
  86. dkim-mirage
  87. dlm
  88. dmarc
  89. dns-certify
  90. dns-cli
  91. dns-client < "7.0.3"
  92. dns-client-lwt
  93. dns-client-mirage
  94. dns-forward
  95. dns-forward-lwt-unix
  96. dns-lwt
  97. dns-mirage
  98. dns-resolver
  99. dns-server
  100. dns-stub
  101. dnssd
  102. docker_hub
  103. docteur >= "0.0.2"
  104. docteur-solo5
  105. docteur-unix >= "0.0.5"
  106. doi2bib
  107. dream
  108. dream-httpaf
  109. dream-pure
  110. dream-serve
  111. dropbox
  112. dune >= "3.17.2"
  113. dune-rpc-lwt
  114. earlybird
  115. elasticsearch-cli
  116. emoji = "2.0.0"
  117. equinoxe
  118. ethernet
  119. ez_api >= "1.2.0"
  120. ezcurl-lwt
  121. ezjs_min < "0.2"
  122. ezjsonm-lwt
  123. ezresto
  124. ezresto-directory >= "0.5"
  125. faraday-lwt
  126. faraday-lwt-unix
  127. fat-filesystem
  128. fiber-lwt
  129. fsevents-lwt
  130. fswatch_lwt
  131. gdbprofiler
  132. git
  133. git-cohttp
  134. git-cohttp-unix
  135. git-kv >= "0.2.0"
  136. git-mirage
  137. git-paf
  138. git-unix >= "3.2.0"
  139. github
  140. github-hooks
  141. github-unix >= "4.4.0"
  142. gitlab-unix
  143. gitlab_pipeline_notifier
  144. gluten-lwt
  145. gluten-lwt-unix < "0.4.0"
  146. gluten-mirage < "0.4.0"
  147. graphql-lwt
  148. gremlin
  149. grpc-lwt
  150. guardian
  151. gufo
  152. h1
  153. h1-lwt-unix
  154. h2-lwt
  155. h2-lwt-unix < "0.10.0"
  156. h2-mirage
  157. happy-eyeballs-lwt
  158. happy-eyeballs-mirage
  159. hidapi-lwt
  160. hiredis >= "0.6"
  161. hl_yaml
  162. hockmd
  163. http-lwt-client
  164. http-mirage-client
  165. http-multipart-formdata >= "2.0.0" & < "3.0.0"
  166. httpaf-lwt-unix
  167. httpun-lwt
  168. httpun-mirage
  169. httpun-ws-lwt
  170. hvsock
  171. i3ipc
  172. influxdb-lwt
  173. inotify >= "2.4"
  174. inquire < "0.3.0"
  175. interface-prime-lwt
  176. ip2location
  177. ip2locationio
  178. ip2whois
  179. ipv6-multicast-lwt
  180. irc-client-lwt
  181. irc-client-lwt-ssl
  182. irc-client-tls
  183. irmin
  184. irmin-bench
  185. irmin-chunk
  186. irmin-cli
  187. irmin-client
  188. irmin-containers
  189. irmin-fs
  190. irmin-git
  191. irmin-graphql
  192. irmin-http
  193. irmin-indexeddb
  194. irmin-layers
  195. irmin-mirage-git
  196. irmin-mirage-graphql
  197. irmin-pack
  198. irmin-server
  199. irmin-test
  200. irmin-unix
  201. irmin-watcher
  202. joolog
  203. jose < "0.9.0"
  204. js_of_ocaml-lwt >= "3.5.0"
  205. jsoo_broadcastchannel
  206. jsoo_storage
  207. jupyter
  208. jupyter-kernel
  209. kafka < "0.5"
  210. kafka_lwt
  211. kappa-library
  212. ke >= "0.5"
  213. kinetic-client
  214. kubecaml
  215. lambda-runtime
  216. lambda_streams_lwt
  217. launchd
  218. ldp
  219. learn-ocaml
  220. learn-ocaml-client
  221. ledgerwallet >= "0.4.0"
  222. letsencrypt
  223. letsencrypt-app
  224. letsencrypt-dns
  225. letters
  226. lichess_api
  227. links >= "0.9.1"
  228. llama
  229. lru_cache
  230. lwt-canceler
  231. lwt-dllist
  232. lwt-exit
  233. lwt-parallel
  234. lwt-pipe
  235. lwt-pipeline
  236. lwt-watcher
  237. lwt_camlp4
  238. lwt_direct
  239. lwt_react
  240. lwt_retry
  241. lwt_ssl
  242. mariadb >= "1.2.0"
  243. markup = "0.7.6"
  244. markup-lwt
  245. mdx
  246. mechaml
  247. mehari-lwt-unix
  248. mehari-mirage
  249. memtrace-mirage
  250. metrics-influx
  251. metrics-lwt
  252. metrics-unix
  253. mimic
  254. mindstorm-lwt
  255. mirage < "4.0.0"
  256. mirage-block >= "2.0.1"
  257. mirage-block-ccm
  258. mirage-block-combinators
  259. mirage-block-lwt
  260. mirage-block-partition
  261. mirage-block-ramdisk
  262. mirage-block-solo5
  263. mirage-block-unikraft
  264. mirage-block-unix >= "2.14.2"
  265. mirage-block-xen
  266. mirage-channel >= "4.0.1"
  267. mirage-channel-lwt
  268. mirage-clock-lwt
  269. mirage-clock-unix < "4.2.0"
  270. mirage-console-lwt
  271. mirage-crypto-rng < "0.11.3"
  272. mirage-crypto-rng-lwt
  273. mirage-crypto-rng-mirage
  274. mirage-device >= "2.0.0"
  275. mirage-flow >= "3.0.0"
  276. mirage-flow-combinators
  277. mirage-flow-lwt
  278. mirage-flow-unix
  279. mirage-fs >= "4.0.0"
  280. mirage-fs-lwt
  281. mirage-kv >= "3.0.1"
  282. mirage-kv-lwt
  283. mirage-kv-unix
  284. mirage-net >= "4.0.0"
  285. mirage-net-lwt
  286. mirage-net-macosx
  287. mirage-net-solo5
  288. mirage-net-unikraft
  289. mirage-net-unix
  290. mirage-net-xen
  291. mirage-profile
  292. mirage-protocols >= "7.0.0"
  293. mirage-protocols-lwt
  294. mirage-qubes
  295. mirage-qubes-ipv4
  296. mirage-runtime
  297. mirage-sleep
  298. mirage-solo5
  299. mirage-stack = "3.0.0"
  300. mirage-stack-lwt
  301. mirage-time >= "3.0.0"
  302. mirage-time-lwt
  303. mirage-time-unix
  304. mirage-types-lwt
  305. mirage-unikraft
  306. mirage-unix
  307. mirage-vnetif
  308. mirage-xen
  309. monorobot
  310. mqtt
  311. mrmime >= "0.5.0"
  312. multipart-form-data
  313. multipart_form >= "0.2.0" & < "0.4.0"
  314. multipart_form-cohttp-lwt < "0.6.0"
  315. multipart_form-lwt
  316. naboris
  317. nbd >= "4.0.3"
  318. nbd-tool
  319. nbd-unix
  320. nottui-lwt
  321. notty-community
  322. nproc
  323. nsq
  324. obuilder
  325. obus >= "1.2.1"
  326. ocluster-api
  327. ocluster-worker
  328. ocplib-resto
  329. ocsigenserver
  330. ocsipersist
  331. ocsipersist-dbm
  332. ocsipersist-lib
  333. ocsipersist-pgsql
  334. ocsipersist-sqlite
  335. octez-distributed-lwt-internal
  336. octez-internal-libs
  337. octez-l2-libs
  338. octez-libs
  339. octez-proto-libs
  340. octez-protocol-compiler
  341. octez-proxy-server
  342. octez-rpc-process
  343. octez-shell-libs
  344. octez-smart-rollup-wasm-benchmark-lib
  345. oframl
  346. ojs_base
  347. omigrate
  348. oneffs
  349. opencage
  350. opentelemetry-client-cohttp-lwt
  351. opentelemetry-cohttp-lwt >= "0.4"
  352. opentelemetry-lwt
  353. opium
  354. opium-graphql
  355. opium_kernel
  356. opomodoro
  357. order-i3-xfce
  358. ordma
  359. oskel >= "0.3.0"
  360. ounit-lwt < "2.2.0"
  361. ounit2-lwt
  362. owork
  363. ozulip
  364. paf
  365. paf-cohttp
  366. passage < "0.1.8"
  367. pcap-format < "0.5.2"
  368. petrol
  369. pgn_parser
  370. pgx_lwt
  371. pgx_lwt_mirage
  372. pgx_lwt_unix < "2.0"
  373. piaf < "0.2.0"
  374. picos_meta
  375. plebeia >= "2.0.0"
  376. plotkicadsch
  377. ppx_defer >= "0.4.0"
  378. ppx_deriving_rpc
  379. ppx_rapper_lwt
  380. proc-smaps
  381. prof_spacetime
  382. prometheus
  383. prometheus-app
  384. promise_jsoo_lwt
  385. protocol-9p
  386. protocol-9p-unix
  387. proton
  388. qcow
  389. qcow-stream
  390. qcow-tool
  391. qcow-types
  392. qfs >= "0.5"
  393. quests
  394. rawlink < "2.1"
  395. rawlink-lwt
  396. rdf_json_ld
  397. rdf_lwt
  398. redis-lwt
  399. reparse-lwt
  400. reparse-lwt-unix
  401. resource-pooling
  402. resp
  403. resp-mirage >= "0.10.0"
  404. resp-unix >= "0.10.0"
  405. resto
  406. resto-cohttp-client >= "0.4"
  407. resto-cohttp-self-serving-client
  408. resto-cohttp-server >= "0.4" & < "0.6" | >= "0.9"
  409. resto-directory >= "0.4"
  410. riak
  411. ringo-lwt
  412. river
  413. rock
  414. rpclib-js
  415. rpclib-lwt
  416. SZXX < "4.0.0"
  417. sanddb
  418. scgi
  419. sendmail-lwt
  420. sendmail-mirage
  421. serial
  422. server-reason-react
  423. session-cohttp-lwt
  424. session-cookie-lwt
  425. session-postgresql-lwt
  426. sessions
  427. shared-block-ring
  428. shared-memory-ring-lwt
  429. sherlodoc
  430. sihl < "0.2.0"
  431. slack
  432. slacko
  433. slipshow
  434. smtml >= "0.7.0"
  435. speed
  436. spin < "0.8.0"
  437. spoke
  438. statocaml
  439. stk
  440. stog
  441. swapfs
  442. syguslib-utils
  443. syndic >= "1.4" & < "1.6.0"
  444. tar-mirage
  445. tar-unix
  446. tcpip
  447. telegraml
  448. terminus
  449. testo-lwt
  450. tezos-base >= "16.0"
  451. tezos-clic >= "16.0"
  452. tezos-crypto >= "16.0"
  453. tezos-crypto-dal
  454. tezos-error-monad >= "16.0"
  455. tezos-lwt-result-stdlib
  456. tezos-p2p = "12.3"
  457. tezos-protocol-environment
  458. tezos-proxy >= "17.3"
  459. tezos-stdlib
  460. tezos-stdlib-unix >= "16.0"
  461. tezos-test-helpers >= "12.3"
  462. tezos-webassembly-interpreter-extra
  463. tidy_email
  464. timmy-lwt
  465. tls >= "0.10.6" & < "0.16.0"
  466. tls-lwt
  467. tls-mirage
  468. tube
  469. tuntap
  470. twirp_cohttp_lwt_unix
  471. uring
  472. uspf
  473. uspf-lwt
  474. uspf-mirage
  475. utop
  476. uwt
  477. vchan
  478. vchan-unix
  479. vchan-xen
  480. vercel
  481. vhd-format-lwt
  482. vmnet
  483. vpnkit
  484. vue-jsoo < "0.3"
  485. wayland < "2.0"
  486. webauthn
  487. xen-evtchn
  488. xen-evtchn-unix
  489. xen-gnt
  490. xen-gnt-unix
  491. xenstore
  492. xenstore-tool
  493. xenstore_transport
  494. xlsx2csv
  495. yocaml_git
  496. yocaml_unix < "2.0.0"
  497. zarr-lwt
  498. zmq-lwt >= "5.2.1"

Conflicts

None