package lwt

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

Install

dune-project
 Dependency

Authors

Maintainers

Sources

5.6.0.tar.gz
md5=e63979ee40a80d5b9e9e5545f33323b4
sha512=d616389bc9e0da11f25843ab7541ac2d40c9543700a89455f14115b339bbe58cef2b8acf0ae97fd54e15a4cb93149cfe1ebfda301aa93933045f76b7d9344160

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

Conflicts

None

Build status

Results of building lwt with each compiler on check.ci.ocaml.org, run of against opam-repository 8a77e58. All results for this package.

Each result links to its build log. To keep crawlers from overloading the site, build logs require a GitHub sign-in (see ocurrent/opam-health-check). On your first log, GitHub asks you to “Authorize opam-health-check”, the OCurrent app that runs these checks; it sees only your public profile, not your repositories. The sign-in lasts 30 days.

This version is not in the latest run; the versions below are.

Version OCaml 4.11 OCaml 4.14 OCaml 5.2 OCaml 5.4 OCaml 5.5
6.1.2 n/a good good good good
5.10.1 good good good good good
  • good Package successfully built
  • partial One of the dependencies failed to build
  • bad Package failed to build
  • n/a Package is not available in this environment
  • internal Internal failure