package lwt

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

Install

dune-project
 Dependency

Authors

Maintainers

Sources

5.2.0.tar.gz
sha256=97c12b3da3d0db019dbdb97950b72f6fd1226ded7519c36ebfe2787574a9b132
md5=d5783fcff4fbfa7f79c9303776e4d144

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

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

  let timeout =
    let%lwt () = 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 -o request example.ml
   ./request *)

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 and BuckleScript.

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

  1. seq
  2. result
  3. ocplib-endian
  4. ocaml >= "4.02.0" & < "4.12"
  5. mmap >= "1.1.0"
  6. dune-configurator
  7. dune >= "1.7.0"
  8. cppo build & >= "1.1.0"

Dev Dependencies (2)

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

Conflicts (1)

  1. ocaml-variants = "4.02.1+BER"