package lwt

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

Install

Dune Dependency

Authors

Maintainers

Sources

4.2.1.tar.gz
sha256=6663be42156c7224c4d2c57842e669d99895dbeb87ebeb46deb90c5a0f0830c1
md5=9d648386ca0a9978eb9487de36b781cc

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: 31 Dec 2020

README

Lwt    version 4.2.1" 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 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_docker < "0.7.1"
  57. current_examples < "0.7.1"
  58. current_git < "0.7.1"
  59. current_github < "0.7.1"
  60. current_gitlab < "0.7.1"
  61. current_ocluster < "0.2"
  62. current_slack < "0.7.1"
  63. current_web < "0.7.1"
  64. DkSDKFFIOCaml_Std
  65. dap
  66. data-encoding < "0.1.1"
  67. datakit-server
  68. devkit
  69. distributed-lwt
  70. dkim-lwt-unix
  71. dkim-mirage
  72. dlm
  73. dns-certify
  74. dns-cli
  75. dns-client < "7.0.3"
  76. dns-client-lwt
  77. dns-client-mirage
  78. dns-forward
  79. dns-forward-lwt-unix
  80. dns-lwt
  81. dns-mirage
  82. dns-resolver
  83. dns-server
  84. dns-stub
  85. dnssd
  86. docker_hub
  87. dream
  88. dream-httpaf
  89. dream-pure
  90. dropbox
  91. dune >= "3.17.2"
  92. elasticsearch-cli
  93. emoji >= "2.0.0"
  94. ethernet
  95. ez_api
  96. ezcurl-lwt
  97. ezjsonm-lwt
  98. ezresto
  99. ezresto-directory
  100. faraday-lwt
  101. faraday-lwt-unix
  102. fat-filesystem
  103. fiber-lwt
  104. fswatch_lwt
  105. fuseau-lwt
  106. gdbprofiler
  107. git
  108. git-paf
  109. git-unix >= "3.2.0" & < "3.15.0"
  110. github
  111. github-hooks
  112. github-unix >= "4.4.0"
  113. gitlab-unix
  114. gluten-lwt < "0.4.0"
  115. gluten-lwt-unix < "0.4.0"
  116. gluten-mirage < "0.4.0"
  117. graphql-lwt
  118. gremlin
  119. gufo
  120. h1
  121. h1-lwt-unix
  122. h2-lwt < "0.10.0"
  123. h2-lwt-unix < "0.10.0"
  124. h2-mirage
  125. happy-eyeballs-lwt
  126. happy-eyeballs-mirage
  127. hiredis
  128. hl_yaml
  129. http-lwt-client
  130. http-multipart-formdata >= "2.0.0" & < "3.0.0"
  131. httpaf-lwt-unix
  132. httpun-lwt
  133. httpun-mirage
  134. httpun-ws-lwt
  135. hvsock
  136. i3ipc
  137. influxdb-lwt
  138. inotify >= "2.4"
  139. inquire < "0.3.0"
  140. interface-prime-lwt
  141. ip2location
  142. ip2locationio
  143. ip2whois
  144. ipv6-multicast-lwt
  145. irc-client-lwt
  146. irc-client-lwt-ssl
  147. irc-client-tls
  148. irmin < "2.7.1"
  149. irmin-indexeddb
  150. irmin-watcher
  151. joolog
  152. jose < "0.9.0"
  153. js_of_ocaml-lwt
  154. jsoo_broadcastchannel
  155. jsoo_storage
  156. jupyter
  157. jupyter-kernel
  158. kafka < "0.5"
  159. kafka_lwt
  160. kappa-library
  161. ke >= "0.5"
  162. kinetic-client
  163. kubecaml
  164. lambda-runtime
  165. lambda-term
  166. launchd
  167. learn-ocaml
  168. learn-ocaml-client
  169. letsencrypt
  170. letsencrypt-app
  171. letsencrypt-dns
  172. links < "0.9.5"
  173. lru_cache
  174. lwt-binio
  175. lwt-canceler
  176. lwt-dllist
  177. lwt-exit
  178. lwt-parallel
  179. lwt-pipe
  180. lwt-pipeline
  181. lwt-watcher
  182. lwt-zmq
  183. lwt_camlp4
  184. lwt_domain < "0.3.0"
  185. lwt_eio < "0.4"
  186. lwt_glib
  187. lwt_log
  188. lwt_named_threads
  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. moonpool-lwt
  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. naboris < "0.1.1"
  258. named-pipe
  259. nbd
  260. nbd-tool
  261. nbd-unix
  262. nocrypto
  263. noise
  264. nottui-lwt
  265. nproc
  266. nsq
  267. obuilder < "0.4"
  268. obus < "1.2.1"
  269. ocluster < "0.2"
  270. ocluster-api < "0.2"
  271. ocplib-resto
  272. ocsigen-start >= "4.1.0" & < "4.7.0"
  273. ocsigenserver
  274. ocsipersist
  275. ocsipersist-dbm
  276. ocsipersist-lib
  277. ocsipersist-pgsql
  278. ocsipersist-sqlite
  279. opam-compiler < "0.2.0"
  280. opam-publish = "0.3.5"
  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
  294. pcap-format < "0.5.2"
  295. pgx_lwt
  296. pgx_lwt_mirage
  297. pgx_lwt_unix < "2.0"
  298. plotkicadsch
  299. ppx_defer >= "0.4.0"
  300. ppx_deriving_rpc
  301. ppx_rapper_lwt
  302. prof_spacetime
  303. prometheus
  304. prometheus-app
  305. promise_jsoo_lwt
  306. protocol-9p
  307. protocol-9p-unix
  308. qcow < "0.12.1"
  309. qcow-tool
  310. qcow-types
  311. qfs >= "0.5"
  312. quests
  313. rawlink < "2.1"
  314. rawlink-lwt
  315. redis-lwt
  316. resource-pooling
  317. resp
  318. resp-mirage >= "0.10.0"
  319. resp-unix >= "0.10.0"
  320. resto
  321. resto-cohttp-client
  322. resto-cohttp-self-serving-client
  323. resto-cohttp-server
  324. resto-directory
  325. riak
  326. ringo-lwt
  327. river
  328. rpclib-js
  329. rpclib-lwt
  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. slacko
  342. slipshow
  343. smtml
  344. speed
  345. spin < "0.6.0"
  346. stog < "0.19.0"
  347. syndic >= "1.4" & < "1.6.0"
  348. tar-unix < "3.3.0"
  349. tcpip
  350. telegraml
  351. tezos-p2p = "12.3"
  352. timmy-lwt
  353. tls < "0.16.0"
  354. tls-lwt < "0.17.5"
  355. tls-mirage
  356. tube
  357. twirp_cohttp_lwt_unix
  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 < "2.4.0"
  377. xenstore_transport
  378. xlsx2csv
  379. zarr-lwt
  380. zbar
  381. zmq-lwt

Conflicts (1)

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

Innovation. Community. Security.