package lwt

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

Install

dune-project
 Dependency

Authors

Maintainers

Sources

5.7.0.tar.gz
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

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. ocaml >= "4.08"
  3. dune-configurator
  4. dune >= "1.8.0"
  5. cppo build & >= "1.1.0"

Dev Dependencies (1)

  1. 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. 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. 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-agents >= "4.1.3"
  215. kappa-binaries >= "4.1.3"
  216. kappa-library
  217. ke >= "0.5"
  218. kinetic-client
  219. kubecaml
  220. lambda-runtime
  221. lambda-term
  222. lambda_streams_lwt
  223. launchd
  224. ldp
  225. learn-ocaml
  226. learn-ocaml-client
  227. ledgerwallet >= "0.4.0"
  228. letsencrypt
  229. letsencrypt-app
  230. letsencrypt-dns
  231. letters
  232. lichess_api
  233. links >= "0.9.1"
  234. linol-lwt
  235. llama
  236. lru_cache
  237. lwt-canceler
  238. lwt-dllist
  239. lwt-exit
  240. lwt-parallel
  241. lwt-pipe
  242. lwt-pipeline
  243. lwt-watcher
  244. lwt_camlp4
  245. lwt_domain
  246. lwt_eio
  247. lwt_glib
  248. lwt_log
  249. lwt_ppx < "6.0.0"
  250. lwt_react
  251. lwt_retry
  252. lwt_ssl
  253. mariadb >= "1.2.0"
  254. markdown_monolith
  255. markup = "0.7.6"
  256. markup-lwt
  257. mdx
  258. mechaml
  259. mehari-lwt-unix
  260. mehari-mirage
  261. memtrace-mirage
  262. metrics-influx
  263. metrics-lwt
  264. metrics-unix
  265. mimic
  266. mindstorm-lwt
  267. mirage < "4.0.0"
  268. mirage-block >= "2.0.1"
  269. mirage-block-ccm
  270. mirage-block-combinators
  271. mirage-block-lwt
  272. mirage-block-partition
  273. mirage-block-ramdisk
  274. mirage-block-solo5
  275. mirage-block-unikraft
  276. mirage-block-unix >= "2.14.2"
  277. mirage-block-xen
  278. mirage-channel >= "4.0.1"
  279. mirage-channel-lwt
  280. mirage-clock-lwt
  281. mirage-clock-unix < "4.2.0"
  282. mirage-console-lwt
  283. mirage-crypto-rng < "0.11.3"
  284. mirage-crypto-rng-lwt
  285. mirage-crypto-rng-mirage
  286. mirage-device >= "2.0.0"
  287. mirage-flow >= "3.0.0"
  288. mirage-flow-combinators
  289. mirage-flow-lwt
  290. mirage-flow-unix
  291. mirage-fs >= "4.0.0"
  292. mirage-fs-lwt
  293. mirage-kv >= "3.0.1"
  294. mirage-kv-lwt
  295. mirage-kv-unix
  296. mirage-net >= "4.0.0"
  297. mirage-net-lwt
  298. mirage-net-macosx
  299. mirage-net-solo5
  300. mirage-net-unikraft
  301. mirage-net-unix
  302. mirage-net-xen
  303. mirage-profile
  304. mirage-protocols >= "7.0.0"
  305. mirage-protocols-lwt
  306. mirage-qubes
  307. mirage-qubes-ipv4
  308. mirage-runtime
  309. mirage-sleep
  310. mirage-solo5
  311. mirage-stack = "3.0.0"
  312. mirage-stack-lwt
  313. mirage-time >= "3.0.0"
  314. mirage-time-lwt
  315. mirage-time-unix
  316. mirage-types-lwt
  317. mirage-unikraft
  318. mirage-unix
  319. mirage-vnetif
  320. mirage-xen
  321. monorobot
  322. moonpool-lwt
  323. mqtt
  324. mrmime >= "0.5.0"
  325. multipart-form-data
  326. multipart_form >= "0.2.0" & < "0.4.0"
  327. multipart_form-cohttp-lwt < "0.6.0"
  328. multipart_form-lwt
  329. mwt
  330. naboris
  331. nbd >= "4.0.3"
  332. nbd-tool
  333. nbd-unix
  334. neo4j_bolt
  335. nocrypto
  336. nottui-lwt
  337. notty-community
  338. nproc
  339. nsq
  340. obuilder
  341. obus >= "1.2.1"
  342. ocluster
  343. ocluster-api
  344. ocluster-worker
  345. ocplib-resto
  346. ocsigenserver
  347. ocsipersist
  348. ocsipersist-dbm
  349. ocsipersist-lib
  350. ocsipersist-pgsql
  351. ocsipersist-sqlite
  352. octez-distributed-lwt-internal
  353. octez-internal-libs
  354. octez-l2-libs
  355. octez-libs
  356. octez-proto-libs
  357. octez-protocol-compiler
  358. octez-proxy-server
  359. octez-rpc-process
  360. octez-shell-libs
  361. octez-smart-rollup-wasm-benchmark-lib
  362. oframl
  363. ojs-base
  364. ojs_base
  365. omigrate
  366. oneffs
  367. opam-check-npm-deps >= "4.1.0"
  368. opencage
  369. opentelemetry-client-cohttp-lwt
  370. opentelemetry-cohttp-lwt >= "0.4"
  371. opentelemetry-lwt
  372. opium
  373. opium-graphql
  374. opium_kernel
  375. opomodoro
  376. order-i3-xfce
  377. ordma
  378. oskel >= "0.3.0"
  379. ounit-lwt < "2.2.0"
  380. ounit2-lwt
  381. owork
  382. ozulip
  383. paf
  384. paf-cohttp
  385. passage < "0.1.8"
  386. pcap-format < "0.5.2"
  387. petrol
  388. pgn_parser
  389. pgx_lwt
  390. pgx_lwt_mirage
  391. pgx_lwt_unix < "2.0"
  392. piaf < "0.2.0"
  393. picos >= "0.3.0" & < "0.5.0"
  394. picos_lwt
  395. picos_meta
  396. plebeia >= "2.0.0"
  397. plist-xml-lwt
  398. plotkicadsch
  399. ppx_defer >= "0.4.0"
  400. ppx_deriving_rpc
  401. ppx_rapper_lwt
  402. proc-smaps
  403. prof_spacetime
  404. prometheus
  405. prometheus-app
  406. promise_jsoo_lwt
  407. protocol-9p
  408. protocol-9p-unix
  409. proton
  410. qcow
  411. qcow-stream
  412. qcow-tool
  413. qcow-types
  414. qdrant < "0.2.0"
  415. qfs >= "0.5"
  416. quests
  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. riak
  434. ringo-lwt
  435. river
  436. rock
  437. rpclib-js
  438. rpclib-lwt
  439. SZXX < "4.0.0"
  440. sanddb
  441. scgi
  442. sendmail-lwt
  443. sendmail-mirage
  444. serial
  445. server-reason-react < "0.4.0"
  446. session-cohttp-lwt
  447. session-cookie-lwt
  448. session-postgresql-lwt
  449. sessions
  450. shared-block-ring
  451. shared-memory-ring-lwt
  452. sherlodoc
  453. sihl < "0.2.0"
  454. slack
  455. slacko
  456. slipshow
  457. smtml >= "0.7.0"
  458. speed
  459. spin < "0.8.0"
  460. spoke
  461. stk
  462. stog
  463. swapfs
  464. syguslib-utils
  465. syndic >= "1.4" & < "1.6.0"
  466. tar-mirage
  467. tar-unix
  468. tcpip
  469. telegraml
  470. terminus
  471. testo-lwt
  472. tezos-base >= "16.0"
  473. tezos-clic >= "16.0"
  474. tezos-crypto >= "16.0"
  475. tezos-crypto-dal
  476. tezos-error-monad >= "16.0"
  477. tezos-lwt-result-stdlib
  478. tezos-p2p = "12.3"
  479. tezos-protocol-environment
  480. tezos-proxy >= "17.3"
  481. tezos-stdlib
  482. tezos-stdlib-unix >= "16.0"
  483. tezos-test-helpers >= "12.3"
  484. tezos-wasmer
  485. tezos-webassembly-interpreter-extra
  486. tezt
  487. tidy_email
  488. timmy-lwt
  489. tls >= "0.10.6" & < "0.16.0"
  490. tls-lwt
  491. tls-mirage
  492. tube
  493. tuntap
  494. twirp_cohttp_lwt_unix
  495. uring
  496. uspf
  497. uspf-lwt
  498. uspf-mirage
  499. utop
  500. uwt
  501. vchan
  502. vchan-unix
  503. vchan-xen
  504. vercel
  505. vhd-format-lwt
  506. vmnet
  507. vpnkit
  508. vue-jsoo < "0.3"
  509. wayland < "2.0"
  510. webauthn
  511. xen-evtchn
  512. xen-evtchn-unix
  513. xen-gnt
  514. xen-gnt-unix
  515. xenstore
  516. xenstore-tool
  517. xenstore_transport
  518. xlsx2csv
  519. yocaml_git
  520. yocaml_unix < "2.0.0"
  521. zarr-lwt
  522. zmq-lwt >= "5.2.1"

Conflicts

None