package lwt

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

Install

dune-project
 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    version 4.5.0 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 with-dev-setup & >= "1.7.3-1"
  2. bisect_ppx with-dev-setup & >= "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. awskit-lwt
  17. awskit-lwt-unix
  18. awskit-s3-lwt
  19. awskit-s3-lwt-unix
  20. awsm-lwt
  21. awso-lwt
  22. azure-cosmos-db < "0.4.0"
  23. azure-cosmos-db-lwt
  24. balancer
  25. bimage-lwt
  26. bistro
  27. brisk-reconciler
  28. builder
  29. bun >= "0.3.3"
  30. cachet-lwt
  31. calculon
  32. caldav
  33. camltc
  34. canary
  35. capnp-rpc-lwt < "1.2.3"
  36. capnp-rpc-unix < "1.2.3"
  37. caqti-lwt < "2.0.1"
  38. carton-git < "0.7.2"
  39. carton-lwt < "1.0.0"
  40. catala-format >= "0.2.0"
  41. cf-lwt
  42. charrua-client
  43. charrua-unix
  44. ciao_lwt
  45. cmdtui-lambda-term
  46. cohttp-lwt < "6.0.0~beta2"
  47. cohttp-lwt-jsoo != "6.1.0" & < "6.2.1"
  48. cohttp-lwt-unix
  49. cohttp-mirage
  50. comby
  51. comby-semantic
  52. conan-lwt
  53. conduit-lwt < "7.0.0"
  54. conduit-lwt-unix < "7.0.0"
  55. cowabloga
  56. crunch
  57. cstruct-lwt
  58. csv-lwt
  59. ctypes >= "0.15.0" & < "0.21.1"
  60. ctypes-foreign >= "0.21.1"
  61. curl_lwt
  62. current < "0.7.1"
  63. current_docker < "0.7.1"
  64. current_examples < "0.7.1"
  65. current_git < "0.7.1"
  66. current_github < "0.7.1"
  67. current_gitlab < "0.7.1"
  68. current_ocluster < "0.2"
  69. current_slack < "0.7.1"
  70. current_web < "0.7.1"
  71. dap
  72. data-encoding < "0.1.1"
  73. datakit-server
  74. devkit
  75. distributed-lwt
  76. dkim-lwt-unix
  77. dkim-mirage
  78. dlm
  79. dns-certify
  80. dns-cli
  81. dns-client < "7.0.3"
  82. dns-client-lwt
  83. dns-client-mirage
  84. dns-forward
  85. dns-forward-lwt-unix
  86. dns-lwt
  87. dns-mirage
  88. dns-resolver
  89. dns-server
  90. dns-stub
  91. dnssd
  92. docker_hub
  93. dream
  94. dream-httpaf
  95. dream-pure
  96. dropbox
  97. dune >= "3.17.2"
  98. elasticsearch-cli
  99. emoji = "2.0.0"
  100. ethernet
  101. ez_api < "3.0.0"
  102. ezcurl-lwt
  103. ezjsonm-lwt
  104. ezresto
  105. ezresto-directory
  106. faraday-lwt
  107. faraday-lwt-unix
  108. fat-filesystem
  109. fiber-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 < "0.4.0"
  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 < "0.10.0"
  129. h2-lwt-unix < "0.10.0"
  130. h2-mirage
  131. happy-eyeballs-lwt
  132. happy-eyeballs-mirage
  133. hiredis
  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
  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.5"
  179. lru_cache
  180. lwt-binio
  181. lwt-canceler
  182. lwt-dllist
  183. lwt-exit
  184. lwt-parallel
  185. lwt-pipe
  186. lwt-pipeline
  187. lwt-watcher
  188. lwt-zmq
  189. lwt_camlp4
  190. lwt_domain < "0.3.0"
  191. lwt_eio < "0.4"
  192. lwt_glib
  193. lwt_log
  194. lwt_named_threads
  195. lwt_ppx < "5.8.0"
  196. lwt_react
  197. lwt_ssl
  198. mariadb >= "1.2.0"
  199. markdown_monolith
  200. markup = "0.7.6"
  201. markup-lwt
  202. mdx
  203. mechaml
  204. metrics-influx
  205. metrics-lwt
  206. metrics-unix
  207. mindstorm-lwt
  208. mirage < "4.0.0"
  209. mirage-block >= "2.0.1"
  210. mirage-block-ccm
  211. mirage-block-combinators
  212. mirage-block-lwt
  213. mirage-block-ramdisk
  214. mirage-block-solo5
  215. mirage-block-unikraft
  216. mirage-block-xen
  217. mirage-channel >= "4.0.1"
  218. mirage-channel-lwt
  219. mirage-clock-lwt
  220. mirage-clock-unix < "4.2.0"
  221. mirage-console-lwt
  222. mirage-crypto-rng < "0.11.3"
  223. mirage-crypto-rng-lwt
  224. mirage-crypto-rng-mirage
  225. mirage-device >= "2.0.0"
  226. mirage-flow >= "3.0.0"
  227. mirage-flow-combinators
  228. mirage-flow-lwt
  229. mirage-flow-unix
  230. mirage-fs >= "4.0.0"
  231. mirage-fs-lwt
  232. mirage-kv >= "3.0.1"
  233. mirage-kv-lwt
  234. mirage-kv-unix < "3.0.0"
  235. mirage-net >= "4.0.0"
  236. mirage-net-lwt
  237. mirage-net-macosx
  238. mirage-net-solo5
  239. mirage-net-unikraft
  240. mirage-net-unix
  241. mirage-net-xen
  242. mirage-profile
  243. mirage-protocols >= "7.0.0"
  244. mirage-protocols-lwt
  245. mirage-runtime
  246. mirage-sleep
  247. mirage-solo5
  248. mirage-stack = "3.0.0"
  249. mirage-stack-lwt
  250. mirage-time >= "3.0.0"
  251. mirage-time-lwt
  252. mirage-time-unix
  253. mirage-types-lwt
  254. mirage-unikraft
  255. mirage-unix
  256. mirage-vnetif
  257. mirage-xen
  258. moonpool-lwt < "0.11"
  259. mrmime >= "0.5.0"
  260. multipart-form-data
  261. multipart_form >= "0.2.0" & < "0.4.0"
  262. multipart_form-lwt < "0.6.0"
  263. mwt
  264. naboris < "0.1.1"
  265. named-pipe
  266. nbd
  267. nbd-tool
  268. nbd-unix
  269. nocrypto
  270. nottui-lwt
  271. notty-community
  272. nproc
  273. nsq
  274. obuilder < "0.4"
  275. obus
  276. ocluster < "0.2"
  277. ocluster-api < "0.2"
  278. ocplib-resto
  279. ocsigen-start >= "4.1.0" & < "4.7.0"
  280. ocsigenserver < "7.0.0"
  281. ocsipersist
  282. ocsipersist-dbm
  283. ocsipersist-lib
  284. ocsipersist-pgsql
  285. ocsipersist-sqlite
  286. opam-compiler < "0.2.0"
  287. opam-publish = "0.3.5" | >= "3.0.0"
  288. opencage
  289. opium < "0.19.0"
  290. opium_kernel
  291. opomodoro
  292. order-i3-xfce
  293. ordma
  294. osc-lwt
  295. oskel >= "0.3.0"
  296. ounit-lwt < "2.2.0"
  297. ounit2-lwt
  298. owork
  299. paf
  300. paf-cohttp
  301. passage < "0.1.8"
  302. pcap-format < "0.5.2"
  303. pgx_lwt
  304. pgx_lwt_mirage
  305. pgx_lwt_unix < "2.0"
  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
  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
  333. resto-cohttp-self-serving-client
  334. resto-cohttp-server
  335. resto-directory
  336. ringo-lwt
  337. river
  338. rpclib-js
  339. rpclib-lwt
  340. sanddb
  341. scgi
  342. sendmail-lwt
  343. sendmail-mirage
  344. serial
  345. session-cohttp-lwt
  346. session-cookie-lwt
  347. session-postgresql-lwt
  348. sessions
  349. shared-block-ring
  350. shared-memory-ring-lwt
  351. slacko
  352. slipshow
  353. smtml >= "0.7.0"
  354. speed
  355. spin < "0.6.0"
  356. stog < "0.19.0"
  357. syndic >= "1.4" & < "1.6.0"
  358. tar-unix < "3.3.0"
  359. tcpip
  360. telegraml
  361. timmy-lwt
  362. tls < "0.16.0"
  363. tls-lwt < "0.17.5"
  364. tls-mirage
  365. tube
  366. twirp_cohttp_lwt_unix
  367. uspf
  368. uspf-lwt
  369. uspf-mirage
  370. utop
  371. uwt
  372. vchan
  373. vchan-unix
  374. vchan-xen
  375. vercel
  376. vhd-format-lwt
  377. vmnet
  378. vue-jsoo < "0.3"
  379. webauthn
  380. xen-evtchn
  381. xen-evtchn-unix
  382. xen-gnt
  383. xen-gnt-unix
  384. xenstore
  385. xenstore-tool
  386. xenstore_transport
  387. xlsx2csv
  388. zarr-lwt
  389. zbar
  390. zmq-lwt

Conflicts (1)

  1. ocaml-variants = "4.02.1+BER"