package lwt

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

Install

dune-project
 Dependency

Authors

Maintainers

Sources

5.5.0.tar.gz
md5=94272fac89c5bf21a89c102b8a8f35a5
sha512=8951b94555e930634375816d71815b9d85daad6ffb7dab24864661504d11be26575ab0b237196c54693efa372a9b69cdc1d5068a20a250dc0bbb4a3c03c5fda1

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: 26 Nov 2021

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 (10)

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

Conflicts (1)

  1. ocaml-variants = "4.02.1+BER"