package lwt

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

Install

dune-project
 Dependency

Authors

Maintainers

Sources

5.3.0.tar.gz
sha256=38ce928378a07b685f4606b60cbe37c26ef93ccb3e808c218e7d34ece9e659ad
md5=85e9c7e9095b4e14d0698e3ece72f378

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 Travis status AppVeyor 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%lwt ... 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 >= "4.02.0" & < "4.12"
  7. mmap >= "1.1.0"
  8. dune-configurator
  9. dune >= "1.8.0"
  10. cppo build & >= "1.1.0"

Dev Dependencies (2)

  1. ocamlfind with-dev-setup & >= "1.7.3-1"
  2. bisect_ppx with-dev-setup & >= "2.0.0"

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

Conflicts (1)

  1. ocaml-variants = "4.02.1+BER"