package lwt

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

Install

Dune Dependency

Authors

Maintainers

Sources

5.4.0.zip
md5=fc4721bdb1a01225b96e3a2debde95fa
sha512=e427f08223b77f9af696c9e6f90ff68e27e02e446910ef90d3da542e7b00bf23dd191ac77c1871288faa2289f8d28fc2f44efc3d3fe9165fe1c7a6be88ee49ff

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

README

Lwt    version" 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:

open Lwt.Syntax

let () =
  let request =
    let* 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* () = write outgoing "GET / HTTP/1.1\r\n" in
      let* () = write outgoing "Connection: close\r\n\r\n" in
      let* response = read incoming in
      Lwt.return (Some response)))
  in

  let timeout =
    let* () = 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 -linkpkg example.ml && ./a.out *)

In the program, functions such as Lwt_io.write create promises. The let* ... 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.

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 (10)

  1. seq
  2. result
  3. ocplib-endian
  4. ocaml-syntax-shims
  5. ocaml >= "4.08.0"
  6. ocaml >= "4.02.0" & < "5.0"
  7. mmap >= "1.1.0"
  8. dune-configurator
  9. dune >= "1.8.0"
  10. cppo build & >= "1.1.0"

Dev Dependencies (1)

  1. ocamlfind dev & >= "1.7.3-1"

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

Conflicts (1)

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

Innovation. Community. Security.