package lwt

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

Install

dune-project
 Dependency

Authors

Maintainers

Sources

4.3.0.tar.gz
sha256=699af62f5ae7d0c19d0b9cc453937e08bc04853e2ff0895e15e86eb3df32d23d
md5=1a72b5ae4245707c12656632a25fc18c

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

Conflicts (1)

  1. ocaml-variants = "4.02.1+BER"