package lwt

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

Install

dune-project
 Dependency

Authors

Maintainers

Sources

5.6.1.tar.gz
md5=279024789a0ec84a9d97d98bad847f97
sha512=698875bd3bfcd5baa47eb48e412f442d289f9972421321541860ebe110b9af1949c3fbc253768495726ec547fe4ba25483cd97ff39bc668496fba95b2ed9edd8

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: 29 Jun 2022

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

Dev Dependencies (1)

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

Conflicts

None