package lwt

Build not checked
  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.

Added to opam-repository:

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

Conflicts (1)

  1. ocaml-variants = "4.02.1+BER"

Build status

Results of building lwt with each compiler on check.ci.ocaml.org, run of against opam-repository 8d8d144. All results for this package.

Each result links to its build log. To keep crawlers from overloading the site, build logs require a GitHub sign-in (see ocurrent/opam-health-check). On your first log, GitHub asks you to “Authorize opam-health-check”, the OCurrent app that runs these checks; it sees only your public profile, not your repositories. The sign-in lasts 30 days.

This version is not in the latest run; the versions below are.

Version OCaml 4.11 OCaml 4.14 OCaml 5.2 OCaml 5.4 OCaml 5.5
6.1.2 n/a good good good good
5.10.1 good good good good good
  • good Package successfully built
  • partial One of the dependencies failed to build
  • bad Package failed to build
  • n/a Package is not available in this environment
  • internal Internal failure