package lwt

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

Install

Dune Dependency

Authors

Maintainers

Sources

4.5.0.tar.gz
sha256=e07d845cd8b105802d975e9d09deee09dcfadbb66d2455d81f09608e42786186
md5=1b2fa7df39a70be1925acdabb8b3f8aa

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

README

Lwt   

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. 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
  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
  101. faraday-lwt
  102. faraday-lwt-unix
  103. fat-filesystem
  104. fiber-lwt
  105. fswatch_lwt
  106. fuseau-lwt
  107. gdbprofiler
  108. git
  109. git-paf
  110. git-unix >= "3.2.0" & < "3.15.0"
  111. github
  112. github-hooks
  113. github-unix >= "4.4.0"
  114. gitlab-unix
  115. gluten-lwt < "0.4.0"
  116. gluten-lwt-unix < "0.4.0"
  117. gluten-mirage < "0.4.0"
  118. graphql-lwt
  119. gremlin
  120. gufo
  121. h1
  122. h1-lwt-unix
  123. h2-lwt < "0.10.0"
  124. h2-lwt-unix < "0.10.0"
  125. h2-mirage
  126. happy-eyeballs-lwt
  127. happy-eyeballs-mirage
  128. hiredis
  129. hl_yaml
  130. http-lwt-client
  131. http-multipart-formdata >= "2.0.0" & < "3.0.0"
  132. httpaf-lwt-unix
  133. httpun-lwt
  134. httpun-mirage
  135. httpun-ws-lwt
  136. hvsock
  137. i3ipc
  138. influxdb-lwt
  139. inotify >= "2.4"
  140. inquire < "0.3.0"
  141. interface-prime-lwt
  142. ip2location
  143. ip2locationio
  144. ip2whois
  145. ipv6-multicast-lwt
  146. irc-client-lwt
  147. irc-client-lwt-ssl
  148. irc-client-tls
  149. irmin < "2.7.1"
  150. irmin-indexeddb
  151. irmin-watcher
  152. joolog
  153. jose < "0.9.0"
  154. js_of_ocaml-lwt
  155. jsoo_broadcastchannel
  156. jsoo_storage
  157. jupyter
  158. jupyter-kernel
  159. kafka < "0.5"
  160. kafka_lwt
  161. kappa-library
  162. ke >= "0.5"
  163. kinetic-client
  164. kubecaml
  165. lambda-runtime
  166. lambda-term
  167. launchd
  168. learn-ocaml
  169. learn-ocaml-client
  170. letsencrypt
  171. letsencrypt-app
  172. letsencrypt-dns
  173. links < "0.9.5"
  174. lru_cache
  175. lwt-binio
  176. lwt-canceler
  177. lwt-dllist
  178. lwt-exit
  179. lwt-parallel
  180. lwt-pipe
  181. lwt-pipeline
  182. lwt-watcher
  183. lwt-zmq
  184. lwt_camlp4
  185. lwt_domain < "0.3.0"
  186. lwt_eio < "0.4"
  187. lwt_glib
  188. lwt_log
  189. lwt_named_threads
  190. lwt_ppx < "5.8.0"
  191. lwt_react
  192. lwt_ssl
  193. mariadb >= "1.2.0"
  194. markup = "0.7.6"
  195. markup-lwt
  196. mdx
  197. mechaml
  198. metrics-influx
  199. metrics-lwt
  200. metrics-unix
  201. mindstorm-lwt
  202. mirage < "4.0.0"
  203. mirage-block >= "2.0.1"
  204. mirage-block-ccm
  205. mirage-block-combinators
  206. mirage-block-lwt
  207. mirage-block-ramdisk
  208. mirage-block-solo5
  209. mirage-block-unikraft
  210. mirage-block-xen
  211. mirage-channel >= "4.0.1"
  212. mirage-channel-lwt
  213. mirage-clock-lwt
  214. mirage-clock-unix < "4.2.0"
  215. mirage-console-lwt
  216. mirage-crypto-rng < "0.11.3"
  217. mirage-crypto-rng-lwt
  218. mirage-crypto-rng-mirage
  219. mirage-device >= "2.0.0"
  220. mirage-flow >= "3.0.0"
  221. mirage-flow-combinators
  222. mirage-flow-lwt
  223. mirage-flow-unix
  224. mirage-fs >= "4.0.0"
  225. mirage-fs-lwt
  226. mirage-kv >= "3.0.1"
  227. mirage-kv-lwt
  228. mirage-kv-unix < "3.0.0"
  229. mirage-net >= "4.0.0"
  230. mirage-net-lwt
  231. mirage-net-macosx
  232. mirage-net-solo5
  233. mirage-net-unikraft
  234. mirage-net-unix
  235. mirage-net-xen
  236. mirage-profile
  237. mirage-protocols >= "7.0.0"
  238. mirage-protocols-lwt
  239. mirage-runtime
  240. mirage-sleep
  241. mirage-solo5
  242. mirage-stack = "3.0.0"
  243. mirage-stack-lwt
  244. mirage-time >= "3.0.0"
  245. mirage-time-lwt
  246. mirage-time-unix
  247. mirage-types-lwt
  248. mirage-unikraft
  249. mirage-unix
  250. mirage-vnetif
  251. mirage-xen
  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 < "0.1.1"
  259. named-pipe
  260. nbd
  261. nbd-tool
  262. nbd-unix
  263. nocrypto
  264. noise
  265. nottui-lwt
  266. nproc
  267. nsq
  268. obuilder < "0.4"
  269. obus
  270. ocluster < "0.2"
  271. ocluster-api < "0.2"
  272. ocplib-resto
  273. ocsigen-start >= "4.1.0" & < "4.7.0"
  274. ocsigenserver
  275. ocsipersist
  276. ocsipersist-dbm
  277. ocsipersist-lib
  278. ocsipersist-pgsql
  279. ocsipersist-sqlite
  280. opam-compiler < "0.2.0"
  281. opam-publish = "0.3.5"
  282. opium < "0.19.0"
  283. opium_kernel
  284. opomodoro
  285. order-i3-xfce
  286. ordma
  287. osc-lwt
  288. oskel >= "0.3.0"
  289. ounit-lwt < "2.2.0"
  290. ounit2-lwt
  291. owork
  292. paf
  293. paf-cohttp
  294. passage
  295. pcap-format < "0.5.2"
  296. pgx_lwt
  297. pgx_lwt_mirage
  298. pgx_lwt_unix < "2.0"
  299. plotkicadsch
  300. ppx_defer >= "0.4.0"
  301. ppx_deriving_rpc
  302. ppx_rapper_lwt
  303. prof_spacetime
  304. prometheus
  305. prometheus-app
  306. promise_jsoo_lwt
  307. protocol-9p
  308. protocol-9p-unix
  309. qcow < "0.12.1"
  310. qcow-tool
  311. qcow-types
  312. qfs >= "0.5"
  313. quests
  314. rawlink < "2.1"
  315. rawlink-lwt
  316. redis-lwt
  317. resource-pooling
  318. resp
  319. resp-mirage >= "0.10.0"
  320. resp-unix >= "0.10.0"
  321. resto
  322. resto-cohttp-client
  323. resto-cohttp-self-serving-client
  324. resto-cohttp-server
  325. resto-directory
  326. riak
  327. ringo-lwt
  328. river
  329. rpclib-js
  330. rpclib-lwt
  331. sanddb
  332. scgi
  333. sendmail-lwt
  334. sendmail-mirage
  335. serial
  336. session-cohttp-lwt
  337. session-cookie-lwt
  338. session-postgresql-lwt
  339. sessions
  340. shared-block-ring
  341. shared-memory-ring-lwt
  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-p2p = "12.3"
  353. timmy-lwt
  354. tls < "0.16.0"
  355. tls-lwt < "0.17.5"
  356. tls-mirage
  357. tube
  358. twirp_cohttp_lwt_unix
  359. uspf
  360. uspf-lwt
  361. uspf-mirage
  362. utop
  363. uwt
  364. vchan
  365. vchan-unix
  366. vchan-xen
  367. vercel
  368. vhd-format-lwt
  369. vmnet
  370. vpnkit
  371. vue-jsoo < "0.3"
  372. webauthn
  373. xen-evtchn
  374. xen-evtchn-unix
  375. xen-gnt
  376. xen-gnt-unix
  377. xenstore
  378. xenstore_transport
  379. xlsx2csv
  380. zarr-lwt
  381. zbar
  382. zmq-lwt

Conflicts (1)

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

Innovation. Community. Security.