package lwt

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

Install

dune-project
 Dependency

Authors

Maintainers

Sources

5.8.0.tar.gz
md5=2682558f405ab7c8638eeb16d0f9f46f
sha512=7f6548a1b1dbfdbc98d9352151ca7be97fa2ab63dbcc429208ce8d08308eee13f7fce31e0cca53f8880233959a60212d622270dd51bf164c3ee272f179769bd9

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 Oct 2024

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. dune >= "1.12"

Dev Dependencies (2)

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

Conflicts

None