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.

Added to opam-repository:

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

Conflicts (1)

  1. ocaml-variants = "4.02.1+BER"