package lwt

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

Install

dune-project
 Dependency

Authors

Maintainers

Sources

6.1.2.tar.gz
md5=00bc7eb2772a28530f7365a7040f0c4f
sha512=a4737a2fc44aaeedca46ec6134c07013d5cf44039a988a6e79a40715a53e43601d4c91bbf11a8e89540528d912b8fa487e3068465c136c1156a872258eb3443b

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 resolved 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:

let () =
  let request =
    let%lwt 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) ->
      write outgoing "GET / HTTP/1.1\r\n";%lwt
      write outgoing "Connection: close\r\n\r\n";%lwt
      let%lwt response = read incoming in
      Lwt.return (Some response)))
  in

  let timeout =
    Lwt_unix.sleep 5.;%lwt
    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,lwt_ppx -linkpkg example.ml && ./a.out *)

If you are not using the lwt_ppx syntax extension, you can use the let* binding opoerators from the Lwt.Syntax module instead.

let () =
  let open Lwt.Syntax in
  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,lwt_ppx -linkpkg example.ml && ./a.out *)

In the program above, functions such as Lwt_io.write create promises. The let%lwt ... in construct (provided by lwt_ppx) or the let* ... in construct (provided by Lwt.Syntax) are used to wait for a promise to resolve. The code after in is scheduled to run after the code inside the let...in has resolved.

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 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. dune-configurator
  3. cppo build & >= "1.1"
  4. ocaml >= "4.14"
  5. dune >= "3.18"

Dev Dependencies (2)

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

Conflicts

None