package lwt

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

Install

Dune Dependency

Authors

Maintainers

Sources

5.3.0.tar.gz
sha256=38ce928378a07b685f4606b60cbe37c26ef93ccb3e808c218e7d34ece9e659ad
md5=85e9c7e9095b4e14d0698e3ece72f378

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: 24 Apr 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%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.

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" & < "4.12"
  7. mmap >= "1.1.0"
  8. dune-configurator
  9. dune >= "1.8.0"
  10. cppo build & >= "1.1.0"

Dev Dependencies (2)

  1. ocamlfind dev & >= "1.7.3-1"
  2. bisect_ppx dev & >= "2.0.0"

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

Conflicts (1)

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

Innovation. Community. Security.