package lwt

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

Install

dune-project
 Dependency

Authors

Maintainers

Sources

5.8.1.tar.gz
md5=d0f824f75ce5297975aec75366fed36c
sha512=c43fff2e9e808dc49ddf09caf7040e512ba36aebe2c600d524095ced11c196c3ac619953a11cb3e3a9f7c6c99e798d43bf179d5f790ab258fb9f746fae4c1125

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: 09 Mar 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 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

Dependencies (5)

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

Dev Dependencies (2)

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

Conflicts

None