package lwt

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

Install

Dune Dependency

Authors

Maintainers

Sources

5.0.1.tar.gz
sha256=b54c78ef2b7a47c94d6ef8c93333b474b2f8ad912f9208cc94dbca44612e9fc9
md5=70d107a5ff1247840e8012b6a3793c15

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: 22 Dec 2019

README

Lwt    version 5.0.1" 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,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 < "0.4.0"
  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 < "0.10.0"
  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. lru_cache
  176. lwt-canceler
  177. lwt-dllist
  178. lwt-exit
  179. lwt-parallel
  180. lwt-pipe
  181. lwt-pipeline
  182. lwt-watcher
  183. lwt_camlp4
  184. lwt_domain < "0.3.0"
  185. lwt_eio < "0.4"
  186. lwt_glib
  187. lwt_log
  188. lwt_ppx < "5.8.0"
  189. lwt_react
  190. lwt_ssl
  191. mariadb >= "1.2.0"
  192. markup = "0.7.6"
  193. markup-lwt
  194. mdx
  195. mechaml
  196. metrics-influx
  197. metrics-lwt
  198. metrics-unix
  199. mindstorm-lwt
  200. mirage < "4.0.0"
  201. mirage-block >= "2.0.1"
  202. mirage-block-ccm
  203. mirage-block-combinators
  204. mirage-block-lwt
  205. mirage-block-ramdisk
  206. mirage-block-solo5
  207. mirage-block-unikraft
  208. mirage-block-xen
  209. mirage-channel >= "4.0.1"
  210. mirage-channel-lwt
  211. mirage-clock-lwt
  212. mirage-clock-unix < "4.2.0"
  213. mirage-console-lwt
  214. mirage-crypto-rng < "0.11.3"
  215. mirage-crypto-rng-lwt
  216. mirage-crypto-rng-mirage
  217. mirage-device >= "2.0.0"
  218. mirage-flow >= "3.0.0"
  219. mirage-flow-combinators
  220. mirage-flow-lwt
  221. mirage-flow-unix
  222. mirage-fs >= "4.0.0"
  223. mirage-fs-lwt
  224. mirage-kv >= "3.0.1"
  225. mirage-kv-lwt
  226. mirage-kv-unix < "3.0.0"
  227. mirage-net >= "4.0.0"
  228. mirage-net-lwt
  229. mirage-net-macosx
  230. mirage-net-solo5
  231. mirage-net-unikraft
  232. mirage-net-unix
  233. mirage-net-xen
  234. mirage-profile
  235. mirage-protocols >= "7.0.0"
  236. mirage-protocols-lwt
  237. mirage-runtime
  238. mirage-sleep
  239. mirage-solo5
  240. mirage-stack = "3.0.0"
  241. mirage-stack-lwt
  242. mirage-time >= "3.0.0"
  243. mirage-time-lwt
  244. mirage-time-unix
  245. mirage-types-lwt
  246. mirage-unikraft
  247. mirage-unix
  248. mirage-vnetif
  249. mirage-xen
  250. moonpool-lwt
  251. mrmime >= "0.5.0"
  252. multipart-form-data
  253. multipart_form >= "0.2.0" & < "0.4.0"
  254. multipart_form-lwt < "0.6.0"
  255. mwt
  256. naboris < "0.1.1"
  257. nbd >= "4.0.3"
  258. nbd-tool
  259. nbd-unix
  260. nocrypto
  261. nottui-lwt
  262. nproc
  263. nsq
  264. obuilder < "0.4"
  265. obus >= "1.2.1"
  266. ocluster < "0.2"
  267. ocluster-api < "0.2"
  268. ocplib-resto
  269. ocsigen-start >= "4.1.0" & < "4.7.0"
  270. ocsigenserver
  271. ocsipersist
  272. ocsipersist-dbm
  273. ocsipersist-lib
  274. ocsipersist-pgsql
  275. ocsipersist-sqlite
  276. opam-compiler < "0.2.0"
  277. opium < "0.19.0"
  278. opium_kernel
  279. opomodoro
  280. order-i3-xfce
  281. ordma
  282. osc-lwt
  283. oskel >= "0.3.0"
  284. ounit-lwt < "2.2.0"
  285. ounit2-lwt
  286. owork
  287. paf
  288. paf-cohttp
  289. passage
  290. pcap-format < "0.5.2"
  291. pgx_lwt
  292. pgx_lwt_mirage
  293. pgx_lwt_unix < "2.0"
  294. plist-xml-lwt
  295. plotkicadsch
  296. ppx_defer >= "0.4.0"
  297. ppx_deriving_rpc
  298. ppx_rapper_lwt
  299. prof_spacetime
  300. prometheus
  301. prometheus-app
  302. promise_jsoo_lwt
  303. protocol-9p
  304. protocol-9p-unix
  305. qcow < "0.12.1"
  306. qcow-tool
  307. qcow-types
  308. qfs >= "0.5"
  309. quests
  310. rawlink < "2.1"
  311. rawlink-lwt
  312. redis-lwt
  313. resource-pooling
  314. resp
  315. resp-mirage >= "0.10.0"
  316. resp-unix >= "0.10.0"
  317. resto
  318. resto-cohttp-client >= "0.4"
  319. resto-cohttp-self-serving-client
  320. resto-cohttp-server >= "0.4"
  321. resto-directory >= "0.4"
  322. riak
  323. ringo-lwt
  324. river
  325. rpclib-js
  326. rpclib-lwt
  327. SZXX < "2.0.0"
  328. sanddb
  329. scgi
  330. sendmail-lwt
  331. sendmail-mirage
  332. serial
  333. session-cohttp-lwt
  334. session-cookie-lwt
  335. session-postgresql-lwt
  336. sessions
  337. shared-block-ring
  338. shared-memory-ring-lwt
  339. slacko
  340. slipshow
  341. smtml
  342. speed
  343. spin < "0.6.0"
  344. stog < "0.19.0"
  345. syndic >= "1.4" & < "1.6.0"
  346. tar-unix < "3.3.0"
  347. tcpip
  348. telegraml
  349. tezos-p2p = "12.3"
  350. timmy-lwt
  351. tls >= "0.10.6" & < "0.16.0"
  352. tls-lwt < "0.17.5"
  353. tls-mirage
  354. tube
  355. tuntap
  356. twirp_cohttp_lwt_unix
  357. uring
  358. uspf
  359. uspf-lwt
  360. uspf-mirage
  361. utop
  362. uwt
  363. vchan
  364. vchan-unix
  365. vchan-xen
  366. vercel
  367. vhd-format-lwt
  368. vmnet
  369. vpnkit
  370. vue-jsoo < "0.3"
  371. webauthn
  372. xen-evtchn
  373. xen-evtchn-unix
  374. xen-gnt
  375. xen-gnt-unix
  376. xenstore
  377. xenstore_transport
  378. xlsx2csv
  379. zarr-lwt
  380. zmq-lwt

Conflicts (1)

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

Innovation. Community. Security.