package lwt

  1. Overview
  2. Docs

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 4.2.0 Gitter chat Travis status AppVeyor status

Lwt is OCaml's concurrent programming library. 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 -package 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 Gitter chat, ask in #ocaml, on discuss.ocaml.org, or on Stack Overflow. Please do ask! Even apparently simple questions often end up educating other users, not to mention enlightening the maintainers!

Release announcements are made in /r/ocaml, and on discuss.ocaml.org. Watching the repo for "Releases only" is also an option.


Contributing

  • We maintain easy starter issues. These are thoroughly explained and hyperlinked. We hope that this makes working on Lwt accessible even to relative OCaml beginners :)
  • 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.
  • The overall development plan can be found in the roadmap.
  • Ask us anything, whether it's about working on Lwt, or any question at all about it :)
  • The documentation always needs proofreading and fixes.
  • Despite a lot of progress, Lwt still needs more tests.
  • 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 (6)

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

Conflicts (1)

  1. ocaml-variants = "4.02.1+BER"