package lwt

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

Install

Dune Dependency

Authors

Maintainers

Sources

5.1.2.tar.gz
sha256=b9fcd49ef391c9848c15818a50488652bd07da2b28a401d7a96558d6db13e3f8
md5=dc4005582a6ab32227f5ff90cb480dbe

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.

Published: 26 Feb 2020

README

Lwt   

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 dev & >= "1.7.3-1"
  2. bisect_ppx dev & >= "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. awsm-lwt
  17. azure-cosmos-db
  18. balancer
  19. bimage-lwt
  20. bistro
  21. brisk-reconciler
  22. brozip
  23. builder
  24. bun >= "0.3.3"
  25. cachet-lwt
  26. calculon
  27. caldav
  28. camltc
  29. canary
  30. capnp-rpc-lwt < "1.2.3"
  31. capnp-rpc-unix < "1.2.3"
  32. caqti-lwt < "2.0.1"
  33. carton-git < "0.7.2"
  34. carton-lwt < "1.0.0"
  35. catala-format >= "0.2.0"
  36. cf-lwt
  37. charrua-client
  38. charrua-unix
  39. cmdtui-lambda-term
  40. cohttp-lwt < "6.0.0~beta2"
  41. cohttp-lwt-jsoo != "6.1.0"
  42. cohttp-lwt-unix
  43. cohttp-mirage
  44. comby
  45. comby-semantic
  46. conan-lwt
  47. conduit-lwt < "7.0.0"
  48. conduit-lwt-unix < "7.0.0"
  49. cowabloga
  50. crunch
  51. cstruct-lwt
  52. csv-lwt
  53. ctypes >= "0.15.0" & < "0.21.1"
  54. ctypes-foreign >= "0.21.1"
  55. curl_lwt
  56. current < "0.7.1"
  57. current_docker < "0.7.1"
  58. current_examples < "0.7.1"
  59. current_git < "0.7.1"
  60. current_github < "0.7.1"
  61. current_gitlab < "0.7.1"
  62. current_ocluster < "0.2"
  63. current_slack < "0.7.1"
  64. current_web < "0.7.1"
  65. DkSDKFFIOCaml_Std
  66. dap
  67. data-encoding < "0.1.1"
  68. datakit-server
  69. devkit >= "1.2"
  70. distributed-lwt
  71. dkim-lwt-unix
  72. dkim-mirage
  73. dlm
  74. dns-certify
  75. dns-cli
  76. dns-client < "7.0.3"
  77. dns-client-lwt
  78. dns-client-mirage
  79. dns-forward
  80. dns-forward-lwt-unix
  81. dns-lwt
  82. dns-mirage
  83. dns-resolver
  84. dns-server
  85. dns-stub
  86. dnssd
  87. docker_hub
  88. dream
  89. dream-httpaf
  90. dream-pure
  91. dropbox
  92. dune >= "3.17.2"
  93. elasticsearch-cli
  94. emoji >= "2.0.0"
  95. ethernet
  96. ez_api
  97. ezcurl-lwt
  98. ezjsonm-lwt
  99. ezresto
  100. ezresto-directory >= "0.5"
  101. faraday-lwt
  102. faraday-lwt-unix
  103. fat-filesystem
  104. fiber-lwt
  105. fsevents-lwt
  106. fswatch_lwt
  107. fuseau-lwt
  108. gdbprofiler
  109. git
  110. git-paf
  111. git-unix >= "3.2.0" & < "3.15.0"
  112. github
  113. github-hooks
  114. github-unix >= "4.4.0"
  115. gitlab-unix
  116. gluten-lwt
  117. gluten-lwt-unix < "0.4.0"
  118. gluten-mirage < "0.4.0"
  119. graphql-lwt
  120. gremlin
  121. gufo
  122. h1
  123. h1-lwt-unix
  124. h2-lwt
  125. h2-lwt-unix < "0.10.0"
  126. h2-mirage
  127. happy-eyeballs-lwt
  128. happy-eyeballs-mirage
  129. hiredis >= "0.6"
  130. hl_yaml
  131. http-lwt-client
  132. http-multipart-formdata >= "2.0.0" & < "3.0.0"
  133. httpaf-lwt-unix
  134. httpun-lwt
  135. httpun-mirage
  136. httpun-ws-lwt
  137. hvsock
  138. i3ipc
  139. influxdb-lwt
  140. inotify >= "2.4"
  141. inquire < "0.3.0"
  142. interface-prime-lwt
  143. ip2location
  144. ip2locationio
  145. ip2whois
  146. ipv6-multicast-lwt
  147. irc-client-lwt
  148. irc-client-lwt-ssl
  149. irc-client-tls
  150. irmin < "2.7.1"
  151. irmin-indexeddb
  152. irmin-watcher
  153. joolog
  154. jose < "0.9.0"
  155. js_of_ocaml-lwt >= "3.5.0"
  156. jsoo_broadcastchannel
  157. jsoo_storage
  158. jupyter
  159. jupyter-kernel
  160. kafka < "0.5"
  161. kafka_lwt
  162. kappa-library
  163. ke >= "0.5"
  164. kinetic-client
  165. kubecaml
  166. lambda-runtime
  167. lambda-term
  168. launchd
  169. learn-ocaml
  170. learn-ocaml-client
  171. letsencrypt
  172. letsencrypt-app
  173. letsencrypt-dns
  174. links >= "0.9.1"
  175. linol-lwt
  176. lru_cache
  177. lwt-canceler
  178. lwt-dllist
  179. lwt-exit
  180. lwt-parallel
  181. lwt-pipe
  182. lwt-pipeline
  183. lwt-watcher
  184. lwt_camlp4
  185. lwt_domain < "0.3.0"
  186. lwt_eio < "0.4"
  187. lwt_glib
  188. lwt_log
  189. lwt_ppx < "5.8.0"
  190. lwt_react
  191. lwt_ssl
  192. mariadb >= "1.2.0"
  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. monorobot
  252. moonpool-lwt
  253. mrmime >= "0.5.0"
  254. multipart-form-data
  255. multipart_form >= "0.2.0" & < "0.4.0"
  256. multipart_form-lwt < "0.6.0"
  257. mwt
  258. naboris
  259. nbd >= "4.0.3"
  260. nbd-tool
  261. nbd-unix
  262. nocrypto
  263. nottui-lwt
  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
  273. ocsipersist
  274. ocsipersist-dbm
  275. ocsipersist-lib
  276. ocsipersist-pgsql
  277. ocsipersist-sqlite
  278. opam-compiler < "0.2.0"
  279. opium < "0.19.0"
  280. opium_kernel
  281. opomodoro
  282. order-i3-xfce
  283. ordma
  284. osc-lwt
  285. oskel >= "0.3.0"
  286. ounit-lwt < "2.2.0"
  287. ounit2-lwt
  288. owork
  289. paf
  290. paf-cohttp
  291. passage
  292. pcap-format < "0.5.2"
  293. pgx_lwt
  294. pgx_lwt_mirage
  295. pgx_lwt_unix < "2.0"
  296. plist-xml-lwt
  297. plotkicadsch
  298. ppx_defer >= "0.4.0"
  299. ppx_deriving_rpc
  300. ppx_rapper_lwt
  301. prof_spacetime
  302. prometheus
  303. prometheus-app
  304. promise_jsoo_lwt
  305. protocol-9p
  306. protocol-9p-unix
  307. qcow < "0.12.1"
  308. qcow-tool
  309. qcow-types
  310. qfs >= "0.5"
  311. quests
  312. rawlink < "2.1"
  313. rawlink-lwt
  314. redis-lwt
  315. resource-pooling
  316. resp
  317. resp-mirage >= "0.10.0"
  318. resp-unix >= "0.10.0"
  319. resto
  320. resto-cohttp-client >= "0.4"
  321. resto-cohttp-self-serving-client
  322. resto-cohttp-server >= "0.4"
  323. resto-directory >= "0.4"
  324. riak
  325. ringo-lwt
  326. river
  327. rpclib-js
  328. rpclib-lwt
  329. SZXX < "2.0.0"
  330. sanddb
  331. scgi
  332. sendmail-lwt
  333. sendmail-mirage
  334. serial
  335. session-cohttp-lwt
  336. session-cookie-lwt
  337. session-postgresql-lwt
  338. sessions
  339. shared-block-ring
  340. shared-memory-ring-lwt
  341. slack
  342. slacko
  343. slipshow
  344. smtml
  345. speed
  346. spin < "0.6.0"
  347. stog < "0.19.0"
  348. syndic >= "1.4" & < "1.6.0"
  349. tar-unix < "3.3.0"
  350. tcpip
  351. telegraml
  352. tezos-lwt-result-stdlib < "12.3"
  353. tezos-p2p = "12.3"
  354. timmy-lwt
  355. tls >= "0.10.6" & < "0.16.0"
  356. tls-lwt < "0.17.5"
  357. tls-mirage
  358. tube
  359. tuntap
  360. twirp_cohttp_lwt_unix
  361. uring
  362. uspf
  363. uspf-lwt
  364. uspf-mirage
  365. utop
  366. uwt
  367. vchan
  368. vchan-unix
  369. vchan-xen
  370. vercel
  371. vhd-format-lwt
  372. vmnet
  373. vpnkit
  374. vue-jsoo < "0.3"
  375. webauthn
  376. xen-evtchn
  377. xen-evtchn-unix
  378. xen-gnt
  379. xen-gnt-unix
  380. xenstore
  381. xenstore_transport
  382. xlsx2csv
  383. zarr-lwt
  384. zmq-lwt

Conflicts (1)

  1. ocaml-variants = "4.02.1+BER"
OCaml

Innovation. Community. Security.