package lwt

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

Install

dune-project
 Dependency

Authors

Maintainers

Sources

5.8.0.tar.gz
md5=2682558f405ab7c8638eeb16d0f9f46f
sha512=7f6548a1b1dbfdbc98d9352151ca7be97fa2ab63dbcc429208ce8d08308eee13f7fce31e0cca53f8880233959a60212d622270dd51bf164c3ee272f179769bd9

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

  1. ocplib-endian
  2. dune-configurator
  3. cppo build & >= "1.1.0"
  4. ocaml >= "4.08" & < "5.5"
  5. dune >= "1.12"

Dev Dependencies (2)

  1. odoc with-doc & >= "2.3.0"
  2. 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 >= "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" & < "1.20261006"
  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-mirage
  122. dream-pure
  123. dream-serve
  124. dropbox
  125. dune >= "3.17.2"
  126. dune-rpc-lwt
  127. earlybird
  128. elasticsearch-cli
  129. emoji = "2.0.0"
  130. equinoxe
  131. ethernet
  132. ez_api >= "1.2.0" & < "3.0.0"
  133. ezcurl-lwt
  134. ezjs_min < "0.2"
  135. ezjsonm-lwt
  136. ezresto
  137. ezresto-directory >= "0.5"
  138. faraday-lwt
  139. faraday-lwt-unix
  140. fat-filesystem
  141. fiber-lwt
  142. fsevents-lwt
  143. fswatch_lwt
  144. fuseau-lwt
  145. fxmacrodata
  146. gdbprofiler
  147. git
  148. git-cohttp
  149. git-cohttp-unix
  150. git-mirage
  151. git-paf
  152. git-unix >= "3.2.0"
  153. github
  154. github-hooks
  155. github-unix >= "4.4.0"
  156. gitlab-unix
  157. gitlab_pipeline_notifier
  158. gluten-lwt
  159. gluten-lwt-unix < "0.4.0"
  160. gluten-mirage < "0.4.0"
  161. gmocoin
  162. granary
  163. granary_repl
  164. graphql-lwt
  165. gremlin
  166. grpc-lwt
  167. guardian
  168. gufo
  169. h1
  170. h1-lwt-unix
  171. h2-lwt
  172. h2-lwt-unix < "0.10.0"
  173. h2-mirage
  174. happy-eyeballs-lwt
  175. happy-eyeballs-mirage
  176. hidapi-lwt
  177. hiredis >= "0.6"
  178. hl_yaml
  179. hockmd
  180. http-lwt-client
  181. http-mirage-client
  182. http-multipart-formdata >= "2.0.0" & < "3.0.0"
  183. httpaf-lwt-unix
  184. httpun-lwt
  185. httpun-mirage
  186. httpun-ws-lwt
  187. hvsock
  188. i3ipc
  189. influxdb-lwt
  190. inotify >= "2.4"
  191. inquire < "0.3.0"
  192. interface-prime-lwt
  193. ip2location
  194. ip2locationio
  195. ip2whois
  196. ipv6-multicast-lwt
  197. irc-client-lwt
  198. irc-client-lwt-ssl
  199. irc-client-tls
  200. irmin
  201. irmin-bench
  202. irmin-chunk
  203. irmin-cli
  204. irmin-client
  205. irmin-containers
  206. irmin-fs
  207. irmin-git
  208. irmin-graphql
  209. irmin-http
  210. irmin-indexeddb
  211. irmin-layers
  212. irmin-mirage-git
  213. irmin-mirage-graphql
  214. irmin-pack
  215. irmin-server
  216. irmin-test
  217. irmin-unix
  218. irmin-watcher
  219. joolog
  220. jose < "0.9.0"
  221. js_of_ocaml-lwt
  222. jsoo_broadcastchannel
  223. jsoo_storage
  224. jupyter
  225. jupyter-kernel
  226. kafka < "0.5"
  227. kafka_lwt
  228. kappa-library
  229. ke >= "0.5"
  230. kinetic-client
  231. kubecaml
  232. lambda-runtime
  233. lambda-term
  234. lambda_streams_lwt
  235. launchd
  236. ldp
  237. learn-ocaml
  238. learn-ocaml-client
  239. ledgerwallet >= "0.4.0"
  240. letsencrypt < "2.0.0"
  241. letsencrypt-app
  242. letsencrypt-dns < "2.0.0"
  243. letters
  244. lichess_api
  245. links
  246. linol-lwt
  247. llama
  248. lru_cache
  249. lwt-canceler
  250. lwt-dllist
  251. lwt-exit
  252. lwt-parallel
  253. lwt-pipe
  254. lwt-pipeline
  255. lwt-watcher
  256. lwt_camlp4
  257. lwt_domain
  258. lwt_eio < "0.6"
  259. lwt_glib
  260. lwt_log
  261. lwt_ppx < "6.0.0~beta01"
  262. lwt_react
  263. lwt_retry
  264. lwt_ssl
  265. mariadb >= "1.2.0"
  266. markdown_monolith
  267. markup = "0.7.6"
  268. markup-lwt
  269. mdx
  270. mechaml
  271. mehari-lwt-unix
  272. mehari-mirage
  273. memtrace-mirage
  274. metrics-influx
  275. metrics-lwt
  276. metrics-unix
  277. migra
  278. mimic
  279. mindstorm-lwt
  280. mirage < "4.0.0"
  281. mirage-block >= "2.0.1"
  282. mirage-block-ccm
  283. mirage-block-combinators
  284. mirage-block-lwt
  285. mirage-block-partition
  286. mirage-block-ramdisk
  287. mirage-block-solo5
  288. mirage-block-unikraft
  289. mirage-block-unix >= "2.14.2"
  290. mirage-block-xen
  291. mirage-channel >= "4.0.1"
  292. mirage-channel-lwt
  293. mirage-clock-lwt
  294. mirage-clock-unix < "4.2.0"
  295. mirage-console-lwt
  296. mirage-crypto-rng < "0.11.3"
  297. mirage-crypto-rng-lwt
  298. mirage-crypto-rng-mirage
  299. mirage-device >= "2.0.0"
  300. mirage-flow >= "3.0.0"
  301. mirage-flow-combinators
  302. mirage-flow-lwt
  303. mirage-flow-unix
  304. mirage-fs >= "4.0.0"
  305. mirage-fs-lwt
  306. mirage-kv >= "3.0.1"
  307. mirage-kv-lwt
  308. mirage-kv-unix
  309. mirage-net >= "4.0.0"
  310. mirage-net-lwt
  311. mirage-net-macosx
  312. mirage-net-solo5
  313. mirage-net-unikraft
  314. mirage-net-unix
  315. mirage-net-xen
  316. mirage-profile
  317. mirage-protocols >= "7.0.0"
  318. mirage-protocols-lwt
  319. mirage-qubes
  320. mirage-qubes-ipv4
  321. mirage-runtime
  322. mirage-sleep
  323. mirage-solo5
  324. mirage-stack = "3.0.0"
  325. mirage-stack-lwt
  326. mirage-time >= "3.0.0"
  327. mirage-time-lwt
  328. mirage-time-unix
  329. mirage-types-lwt
  330. mirage-unikraft
  331. mirage-unix
  332. mirage-vnetif
  333. mirage-xen
  334. monorobot
  335. moonpool-lwt
  336. mqtt
  337. mrmime >= "0.5.0"
  338. multipart-form-data
  339. multipart_form >= "0.2.0" & < "0.4.0"
  340. multipart_form-cohttp-lwt < "0.6.0"
  341. multipart_form-lwt
  342. mwt
  343. naboris
  344. nbd >= "4.0.3"
  345. nbd-tool
  346. nbd-unix
  347. neo4j_bolt
  348. nocrypto
  349. nottui-lwt
  350. notty-community
  351. nproc
  352. nsq
  353. obuilder
  354. obus >= "1.2.1"
  355. ocluster
  356. ocluster-api
  357. ocluster-worker
  358. ocplib-resto
  359. ocsigenserver
  360. ocsipersist
  361. ocsipersist-dbm
  362. ocsipersist-lib
  363. ocsipersist-pgsql
  364. ocsipersist-sqlite
  365. oframl
  366. ohttp-cohttp-lwt
  367. ojs_base
  368. omigrate
  369. oneffs
  370. opam-check-npm-deps >= "4.1.0"
  371. opam-publish >= "3.0.0"
  372. opencage
  373. opentelemetry-client-cohttp-lwt
  374. opentelemetry-client-ocurl-lwt
  375. opentelemetry-cohttp-lwt >= "0.4"
  376. opentelemetry-lwt
  377. opium
  378. opium-graphql
  379. opium_kernel
  380. opomodoro
  381. order-i3-xfce
  382. ordma
  383. oskel >= "0.3.0"
  384. ounit-lwt < "2.2.0"
  385. ounit2-lwt
  386. owork
  387. ozulip
  388. paf
  389. paf-cohttp
  390. passage < "0.1.8"
  391. pcap-format < "0.5.2"
  392. petrol
  393. pgn_parser
  394. pgx_lwt
  395. pgx_lwt_mirage
  396. pgx_lwt_unix < "2.0"
  397. piaf < "0.2.0"
  398. picos >= "0.3.0" & < "0.5.0"
  399. picos_lwt
  400. picos_meta
  401. plebeia >= "2.0.0"
  402. plist-xml-lwt
  403. plotkicadsch
  404. posix-getopt >= "4.0.2"
  405. ppx_defer >= "0.4.0"
  406. ppx_deriving_rpc
  407. ppx_rapper_lwt
  408. proc-smaps
  409. prof_spacetime
  410. prometheus < "2.0"
  411. prometheus-app
  412. prometheus-lwt
  413. promise_jsoo_lwt
  414. protocol-9p
  415. protocol-9p-unix
  416. proton
  417. pxshot
  418. qcow
  419. qcow-stream
  420. qcow-tool
  421. qcow-types
  422. qdrant < "0.2.0"
  423. qfs
  424. quests
  425. quickterface
  426. rawlink < "2.1"
  427. rawlink-lwt
  428. rdf_json_ld
  429. rdf_lwt
  430. redis-lwt
  431. reparse-lwt
  432. reparse-lwt-unix
  433. resource-pooling
  434. resp
  435. resp-mirage >= "0.10.0"
  436. resp-unix >= "0.10.0"
  437. resto
  438. resto-cohttp-client >= "0.4"
  439. resto-cohttp-self-serving-client
  440. resto-cohttp-server >= "0.4" & < "0.6" | >= "0.9"
  441. resto-directory >= "0.4"
  442. ringo-lwt
  443. river
  444. rock
  445. rpclib-js
  446. rpclib-lwt
  447. SZXX < "4.0.0"
  448. sanddb
  449. scgi
  450. sendmail-lwt
  451. sendmail-mirage
  452. serial
  453. server-reason-react < "0.4.0"
  454. session-cohttp-lwt
  455. session-cookie-lwt
  456. session-postgresql-lwt
  457. sessions
  458. shared-block-ring
  459. shared-memory-ring-lwt
  460. sherlodoc
  461. sihl < "0.2.0"
  462. slack
  463. slacko
  464. slipshow < "0.12.0"
  465. smtml >= "0.7.0"
  466. speed
  467. spin < "0.8.0"
  468. spoke
  469. stk
  470. stog
  471. swapfs
  472. syguslib-utils
  473. syndic >= "1.4" & < "1.6.0"
  474. tar-mirage
  475. tar-unix
  476. tcpip
  477. telegraml
  478. terminus
  479. testcontainers
  480. testo-lwt
  481. tezt
  482. tidy_email
  483. timmy-lwt
  484. tls >= "0.10.6" & < "0.16.0"
  485. tls-lwt
  486. tls-mirage
  487. tube
  488. tuntap
  489. twirp_cohttp_lwt_unix
  490. uring
  491. uspf
  492. uspf-lwt
  493. uspf-mirage
  494. utcp
  495. utop
  496. uwt
  497. vchan
  498. vchan-unix
  499. vchan-xen
  500. vercel
  501. vhd-format-lwt
  502. vmnet
  503. vue-jsoo < "0.3"
  504. wayland < "2.0"
  505. webauthn
  506. xen-evtchn
  507. xen-evtchn-unix
  508. xen-gnt
  509. xen-gnt-unix
  510. xenstore
  511. xenstore-tool
  512. xenstore_transport
  513. xlsx2csv
  514. yocaml_git
  515. yocaml_unix < "2.0.0"
  516. zarr-lwt
  517. zmq-lwt >= "5.2.1"

Conflicts

None