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" & < "5.5"
  6. ocaml ("os" != "win32" & >= "4.02.0" | >= "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 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
  11. anthill
  12. anycache-lwt
  13. archi-lwt
  14. arp
  15. awa-mirage
  16. aws-lwt
  17. aws-s3-lwt
  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 < "0.2.0"
  34. bun >= "0.3.3"
  35. cachet-lwt
  36. calculon
  37. caldav
  38. camltc
  39. canary
  40. capnp-rpc-lwt < "1.2.3"
  41. capnp-rpc-unix < "1.2.3"
  42. caqti-lwt
  43. caqti-mirage
  44. carton < "1.0.0"
  45. carton-git < "0.7.2"
  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. ciao_lwt
  55. clz
  56. cmdtui-lambda-term
  57. cohttp-curl-lwt
  58. cohttp-lwt < "6.0.0"
  59. cohttp-lwt-jsoo != "6.1.0" & < "6.2.1"
  60. cohttp-lwt-unix
  61. cohttp-mirage
  62. cohttp-server-lwt-unix
  63. comby
  64. comby-semantic
  65. conan-lwt
  66. conduit-lwt < "7.0.0"
  67. conduit-lwt-unix < "7.0.0"
  68. cowabloga
  69. crunch
  70. cstruct-lwt
  71. csv-lwt
  72. ctypes >= "0.15.0" & < "0.21.1"
  73. ctypes-foreign >= "0.21.1"
  74. curl_lwt
  75. current < "0.7.1"
  76. current_docker < "0.7.1"
  77. current_examples < "0.7.1"
  78. current_git < "0.7.1"
  79. current_github < "0.7.1"
  80. current_gitlab < "0.7.1"
  81. current_ocluster < "0.2"
  82. current_rpc < "0.6.4"
  83. current_slack < "0.7.1"
  84. current_web < "0.7.1"
  85. dap
  86. data-encoding < "0.1.1"
  87. datakit-server
  88. devkit >= "1.2"
  89. distributed-lwt
  90. dkim-bin < "0.8.0"
  91. dkim-lwt-unix
  92. dkim-mirage
  93. dlm
  94. dmarc
  95. dns-certify
  96. dns-cli
  97. dns-client < "7.0.3"
  98. dns-client-lwt
  99. dns-client-mirage
  100. dns-forward
  101. dns-forward-lwt-unix
  102. dns-lwt
  103. dns-mirage
  104. dns-resolver
  105. dns-server
  106. dns-stub
  107. dnsrobot
  108. dnssd
  109. docker_hub
  110. docteur
  111. docteur-solo5
  112. docteur-unix
  113. doi2bib
  114. dream
  115. dream-httpaf
  116. dream-pure
  117. dream-serve
  118. dropbox
  119. dune >= "3.17.2"
  120. earlybird
  121. elasticsearch-cli
  122. emoji = "2.0.0"
  123. equinoxe
  124. ethernet
  125. ez_api < "3.0.0"
  126. ezcurl-lwt
  127. ezjs_min < "0.2"
  128. ezjsonm-lwt
  129. ezresto
  130. ezresto-directory >= "0.5"
  131. faraday-lwt
  132. faraday-lwt-unix
  133. fat-filesystem
  134. fiber-lwt
  135. fsevents-lwt
  136. fswatch_lwt
  137. fuseau-lwt
  138. gdbprofiler
  139. git
  140. git-cohttp
  141. git-cohttp-unix
  142. git-mirage
  143. git-paf
  144. git-unix >= "3.2.0" & < "3.15.0"
  145. github
  146. github-hooks
  147. github-unix >= "4.4.0"
  148. gitlab-unix
  149. gluten-lwt
  150. gluten-lwt-unix < "0.4.0"
  151. gluten-mirage < "0.4.0"
  152. graphql-lwt
  153. gremlin
  154. grpc-lwt
  155. gufo
  156. h1
  157. h1-lwt-unix
  158. h2-lwt
  159. h2-lwt-unix < "0.10.0"
  160. h2-mirage
  161. happy-eyeballs-lwt
  162. happy-eyeballs-mirage
  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-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
  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. letsencrypt < "2.0.0"
  225. letsencrypt-app
  226. letsencrypt-dns < "2.0.0"
  227. letters
  228. links
  229. linol-lwt
  230. llama
  231. lru_cache
  232. lwt-canceler
  233. lwt-dllist
  234. lwt-exit
  235. lwt-parallel
  236. lwt-pipe
  237. lwt-pipeline
  238. lwt-watcher
  239. lwt_camlp4
  240. lwt_domain < "0.3.0"
  241. lwt_eio < "0.4"
  242. lwt_glib
  243. lwt_log
  244. lwt_ppx < "5.8.0"
  245. lwt_react
  246. lwt_retry
  247. lwt_ssl
  248. mariadb >= "1.2.0"
  249. markdown_monolith
  250. markup = "0.7.6"
  251. markup-lwt
  252. mdx
  253. mechaml
  254. memtrace-mirage
  255. metrics-influx
  256. metrics-lwt
  257. metrics-unix
  258. mimic
  259. mindstorm-lwt
  260. mirage < "4.0.0"
  261. mirage-block >= "2.0.1"
  262. mirage-block-ccm
  263. mirage-block-combinators
  264. mirage-block-lwt
  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 < "0.5"
  329. obus >= "1.2.1"
  330. ocluster < "0.2"
  331. ocluster-api < "0.2"
  332. ocplib-resto
  333. ocsigen-start >= "4.1.0" & < "4.7.0"
  334. ocsigenserver < "7.0.0"
  335. ocsipersist
  336. ocsipersist-dbm
  337. ocsipersist-lib
  338. ocsipersist-pgsql
  339. ocsipersist-sqlite
  340. oframl
  341. ojs-base
  342. ojs_base
  343. omigrate
  344. oneffs
  345. opam-check-npm-deps >= "4.1.0"
  346. opam-compiler < "0.2.0"
  347. opam-publish >= "3.0.0"
  348. opencage
  349. opentelemetry-client-cohttp-lwt
  350. opentelemetry-cohttp-lwt >= "0.4"
  351. opentelemetry-lwt
  352. opium
  353. opium-graphql
  354. opium_kernel
  355. opomodoro
  356. order-i3-xfce
  357. ordma
  358. osc-lwt
  359. oskel >= "0.3.0"
  360. ounit-lwt < "2.2.0"
  361. ounit2-lwt
  362. owork
  363. ozulip
  364. paf
  365. paf-cohttp
  366. passage < "0.1.8"
  367. pcap-format < "0.5.2"
  368. petrol
  369. pgx_lwt
  370. pgx_lwt_mirage
  371. pgx_lwt_unix < "2.0"
  372. piaf < "0.2.0"
  373. plebeia >= "2.0.0"
  374. plist-xml-lwt
  375. plotkicadsch
  376. posix-getopt >= "4.0.2"
  377. ppx_defer >= "0.4.0"
  378. ppx_deriving_rpc
  379. ppx_rapper_lwt
  380. proc-smaps
  381. prof_spacetime
  382. prometheus
  383. prometheus-app
  384. promise_jsoo_lwt
  385. protocol-9p
  386. protocol-9p-unix
  387. proton
  388. qcow
  389. qcow-stream
  390. qcow-tool
  391. qcow-types
  392. qfs
  393. quests
  394. quickterface
  395. rawlink < "2.1"
  396. rawlink-lwt
  397. rdf_json_ld < "1.0.0"
  398. rdf_lwt < "1.0.0"
  399. redis-lwt
  400. reparse-lwt
  401. reparse-lwt-unix
  402. resource-pooling
  403. resp
  404. resp-mirage >= "0.10.0"
  405. resp-unix >= "0.10.0"
  406. resto
  407. resto-cohttp-client >= "0.4"
  408. resto-cohttp-self-serving-client
  409. resto-cohttp-server >= "0.4"
  410. resto-directory >= "0.4"
  411. ringo-lwt
  412. river
  413. rock
  414. rpclib-js
  415. rpclib-lwt
  416. SZXX < "4.0.0"
  417. sanddb
  418. scgi
  419. sendmail-lwt
  420. sendmail-mirage
  421. serial
  422. session-cohttp-lwt
  423. session-cookie-lwt
  424. session-postgresql-lwt
  425. sessions
  426. shared-block-ring
  427. shared-memory-ring-lwt
  428. sihl < "0.2.0"
  429. slack
  430. slacko
  431. slipshow
  432. smtml >= "0.7.0"
  433. speed
  434. spin < "0.8.0"
  435. stk
  436. stog < "1.0.0"
  437. syguslib-utils
  438. syndic >= "1.4" & < "1.6.0"
  439. tar-unix < "3.3.0"
  440. tcpip
  441. telegraml
  442. terminus
  443. tezt < "3.0.0"
  444. tidy_email
  445. timmy-lwt
  446. tls >= "0.10.6" & < "0.16.0"
  447. tls-lwt < "0.17.5"
  448. tls-mirage
  449. tube
  450. tuntap
  451. twirp_cohttp_lwt_unix
  452. uring
  453. uspf
  454. uspf-lwt
  455. uspf-mirage
  456. utcp
  457. utop
  458. uwt
  459. vchan
  460. vchan-unix
  461. vchan-xen
  462. vercel
  463. vhd-format-lwt
  464. vmnet
  465. vue-jsoo < "0.3"
  466. wayland < "2.0"
  467. webauthn
  468. xen-evtchn
  469. xen-evtchn-unix
  470. xen-gnt
  471. xen-gnt-unix
  472. xenstore
  473. xenstore-tool
  474. xenstore_transport
  475. xlsx2csv
  476. yocaml_git < "2.0.0"
  477. yocaml_unix < "2.0.0"
  478. zarr-lwt
  479. zmq-lwt

Conflicts (1)

  1. ocaml-variants = "4.02.1+BER"