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

Conflicts

None