package lwt

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

Install

dune-project
 Dependency

Authors

Maintainers

Sources

5.1.1.tar.gz
sha256=078a6c351d94d6eeb827c62a25c87ac11715c791f4b642d85303740c2cac8d3f
md5=4ddec0f42b7aa4a310175a14c47c60a3

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: 07 Jan 2020

README

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

Conflicts (1)

  1. ocaml-variants = "4.02.1+BER"