package lwt

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

Install

Dune Dependency

Authors

Maintainers

Sources

5.2.0.tar.gz
sha256=97c12b3da3d0db019dbdb97950b72f6fd1226ded7519c36ebfe2787574a9b132
md5=d5783fcff4fbfa7f79c9303776e4d144

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: 09 Mar 2020

README

Lwt   

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:

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 dev & >= "1.7.3-1"
  2. bisect_ppx dev & >= "1.3.0"

  1. 0install >= "2.15.1"
  2. albatross
  3. alcotest-lwt
  4. alcotest-mirage
  5. ambient-context-lwt
  6. amqp-client >= "0.9.0" & < "1.0.2" | >= "1.1.0"
  7. amqp-client-lwt >= "2.0.1"
  8. angstrom-lwt-unix
  9. anthill
  10. anycache-lwt
  11. arakoon < "1.8.6" | >= "1.8.8"
  12. archi-lwt
  13. arp >= "2.3.1"
  14. arp-mirage >= "2.2.1"
  15. awa-lwt
  16. awa-mirage < "0.2.0"
  17. aws-lwt
  18. aws-s3-lwt
  19. awsm-lwt
  20. azure-cosmos-db
  21. baardskeerder
  22. balancer
  23. bap < "1.0.0"
  24. bap-server < "0.2.0"
  25. bastet_lwt
  26. bimage-lwt
  27. biocaml = "0.4.0"
  28. bistro >= "0.4.0"
  29. brozip
  30. builder
  31. bun >= "0.3.3"
  32. c3
  33. calculon
  34. caldav
  35. camltc = "0.9.5" | >= "0.9.7.0"
  36. canary
  37. capnp-rpc-lwt < "1.2.3"
  38. capnp-rpc-unix >= "0.9.0" & < "1.2.3"
  39. caqti-lwt >= "0.11.0" & < "2.0.1"
  40. carton-git < "0.7.2"
  41. carton-lwt
  42. cf-lwt
  43. channel
  44. charrua-client >= "1.3.0"
  45. charrua-client-lwt
  46. charrua-client-mirage
  47. charrua-core < "0.3"
  48. charrua-unix >= "0.3" & != "0.10"
  49. cmdtui-lambda-term
  50. coclobas
  51. cohttp-lwt < "6.0.0~alpha2"
  52. cohttp-lwt-jsoo
  53. cohttp-lwt-unix >= "1.1.1"
  54. cohttp-lwt-unix-nossl
  55. cohttp-lwt-unix-ssl
  56. cohttp-mirage
  57. comby
  58. comby-semantic
  59. conan-lwt
  60. conduit-lwt < "7.0.0"
  61. conduit-lwt-unix < "7.0.0"
  62. cowabloga >= "0.2.2"
  63. crunch >= "2.0.0"
  64. cstruct-lwt
  65. csv-lwt
  66. csvprovider
  67. ctypes >= "0.15.0" & < "0.21.1"
  68. ctypes-foreign >= "0.21.1"
  69. current < "0.6.4"
  70. current_docker < "0.6.4"
  71. current_examples < "0.6.4"
  72. current_git < "0.6.4"
  73. current_github < "0.6.4"
  74. current_gitlab < "0.6.4"
  75. current_ocluster < "0.2"
  76. current_slack < "0.6.4"
  77. current_web < "0.6.4"
  78. DkSDKFFIOCaml_Std
  79. dap
  80. data-encoding < "0.1.1"
  81. datakit
  82. datakit-bridge-github
  83. datakit-bridge-local-git
  84. datakit-ci
  85. datakit-client >= "0.11.0"
  86. datakit-github
  87. datakit-server
  88. devkit >= "1.2"
  89. dht < "0.2.0"
  90. distributed-lwt
  91. dkim-mirage
  92. dlm
  93. dns >= "0.19.1" & < "0.20.1"
  94. dns-certify
  95. dns-cli >= "4.6.3"
  96. dns-client < "7.0.0"
  97. dns-client-lwt
  98. dns-client-mirage
  99. dns-forward >= "0.9.0"
  100. dns-forward-lwt-unix
  101. dns-lwt
  102. dns-mirage
  103. dns-resolver
  104. dns-server
  105. dns-stub
  106. dnssd
  107. docker_hub
  108. dream
  109. dream-httpaf
  110. dream-pure
  111. dropbox
  112. dune_watch
  113. earlybird < "1.0.0"
  114. elasticsearch-cli >= "0.4"
  115. eris-lwt
  116. ethernet
  117. ez_api
  118. ezcurl-lwt
  119. ezirmin
  120. ezjs_min < "0.2"
  121. ezjsonm >= "0.4.2" & < "0.5.0"
  122. ezjsonm-lwt
  123. ezresto
  124. ezresto-directory >= "0.5"
  125. faraday-lwt
  126. faraday-lwt-unix >= "0.6.0"
  127. fat-filesystem >= "0.12.0"
  128. fiber-lwt
  129. flowtype >= "0.72.0"
  130. frenetic < "2.0.0"
  131. fsevents-lwt
  132. fswatch_lwt
  133. fuseau-lwt
  134. gamepad
  135. gdb
  136. gdbprofiler >= "0.3"
  137. git != "1.4.3" & != "1.7.2"
  138. git-paf
  139. git-unix = "1.11.1" | >= "3.0.0" & < "3.10.0"
  140. github
  141. github-hooks < "0.2.0" | >= "0.4.0"
  142. github-unix >= "4.4.0"
  143. gitlab-unix
  144. gluten-lwt
  145. gluten-lwt-unix < "0.4.0"
  146. gluten-mirage < "0.4.0"
  147. graphql-lwt
  148. gremlin
  149. gufo
  150. h1
  151. h1-lwt-unix
  152. h2-lwt
  153. h2-lwt-unix < "0.10.0"
  154. h2-mirage
  155. happy-eyeballs-lwt
  156. happy-eyeballs-mirage
  157. hardcaml < "1.1.0"
  158. hardcaml-examples >= "0.3.0"
  159. hardcaml-framework
  160. hiredis != "0.4"
  161. hl_yaml
  162. horned_worm < "0.3.1"
  163. http-lwt-client
  164. http-multipart-formdata >= "2.0.0" & < "3.0.0"
  165. http2https
  166. httpaf-lwt-unix
  167. httpun-lwt
  168. httpun-mirage
  169. httpun-ws-lwt
  170. hvsock >= "1.0.2"
  171. i3ipc >= "0.1.4"
  172. imaplet-lwt
  173. influxdb-lwt
  174. inotify >= "2.4"
  175. inquire < "0.3.0"
  176. interface-prime-lwt
  177. iocaml < "0.4.6"
  178. iocaml-kernel >= "0.4.3" & < "0.4.6"
  179. iocamljs-kernel
  180. ip2location
  181. ip2locationio
  182. ip2whois
  183. ipv6-multicast-lwt
  184. irc-client-lwt
  185. irc-client-lwt-ssl
  186. irc-client-tls
  187. irmin < "0.9.6" | = "0.9.10" | >= "0.11.0" & < "2.7.0"
  188. irmin-bench < "2.7.0"
  189. irmin-chunk < "2.7.0"
  190. irmin-containers < "2.7.0"
  191. irmin-fs >= "2.3.0" & < "2.7.0"
  192. irmin-git >= "2.3.0" & < "2.7.0"
  193. irmin-graphql >= "2.3.0" & < "2.7.0"
  194. irmin-http >= "2.3.0" & < "2.7.0"
  195. irmin-indexeddb
  196. irmin-layers < "2.7.0"
  197. irmin-mem >= "2.3.0"
  198. irmin-mirage-git >= "2.3.0" & < "2.7.0"
  199. irmin-mirage-graphql >= "2.3.0" & < "2.7.0"
  200. irmin-pack < "2.7.0"
  201. irmin-test >= "2.3.0" & < "2.7.0"
  202. irmin-unix >= "2.3.0" & < "2.7.0"
  203. irmin-watcher >= "0.3.0"
  204. jerboa
  205. jitsu
  206. joolog
  207. jose < "0.9.0"
  208. js_of_ocaml < "2.5"
  209. js_of_ocaml-lwt >= "3.5.0"
  210. jsoo_broadcastchannel
  211. jsoo_router
  212. jsoo_storage
  213. jupyter >= "2.3.0"
  214. jupyter-kernel >= "0.4"
  215. kafka >= "0.3" & < "0.5"
  216. kafka_lwt
  217. kappa-library
  218. ke >= "0.5"
  219. ketrew >= "3.2.0"
  220. kinetic-client < "0.0.3" | >= "0.0.9"
  221. kubecaml
  222. lablqml < "0.6"
  223. lambda-runtime
  224. lambda-term >= "1.13"
  225. lambda_streams_lwt
  226. launchd
  227. learn-ocaml >= "0.13.0"
  228. learn-ocaml-client >= "0.13.0"
  229. letsencrypt
  230. letsencrypt-app
  231. letsencrypt-dns
  232. letters
  233. libres3
  234. links != "0.9"
  235. linol-lwt
  236. lru_cache < "v0.16.0"
  237. lwt-binio < "0.2.0"
  238. lwt-canceler
  239. lwt-dllist
  240. lwt-exit
  241. lwt-parallel >= "1.0.0"
  242. lwt-pipe
  243. lwt-pipeline
  244. lwt-watcher
  245. lwt-zmq < "1.0.0"
  246. lwt_camlp4
  247. lwt_domain < "0.3.0"
  248. lwt_eio < "0.4"
  249. lwt_glib >= "1.0.1"
  250. lwt_log >= "1.1.0"
  251. lwt_ppx
  252. lwt_ppx_let
  253. lwt_react >= "1.0.1"
  254. lwt_ssl >= "1.0.1"
  255. mariadb < "0.5.1"
  256. markup = "0.7.6"
  257. markup-lwt
  258. mdx
  259. mechaml
  260. metrics-influx
  261. metrics-lwt
  262. metrics-mirage
  263. metrics-unix
  264. mindstorm-lwt
  265. mirage >= "0.4.1" & != "0.6.1" & < "0.8.0" | >= "0.10.0" & < "2.7.0"
  266. mirage-block < "1.0.0" | >= "2.0.0"
  267. mirage-block-ccm
  268. mirage-block-combinators
  269. mirage-block-lwt
  270. mirage-block-ramdisk
  271. mirage-block-solo5
  272. mirage-block-unix < "2.3.0" | = "2.8.2"
  273. mirage-block-xen
  274. mirage-bootvar-solo5 >= "0.2.0"
  275. mirage-bootvar-unix
  276. mirage-bootvar-xen >= "0.4.0"
  277. mirage-channel >= "4.0.0"
  278. mirage-channel-lwt
  279. mirage-clock-freestanding < "3.0.0"
  280. mirage-clock-lwt
  281. mirage-clock-unix >= "1.3.0" & < "3.0.0"
  282. mirage-console >= "3.0.0"
  283. mirage-console-lwt
  284. mirage-console-solo5 >= "0.2.0"
  285. mirage-console-unix >= "2.2.1"
  286. mirage-console-xen >= "5.0.0"
  287. mirage-console-xen-backend
  288. mirage-console-xen-cli
  289. mirage-crypto-entropy
  290. mirage-crypto-rng >= "0.7.0" & < "0.11.0"
  291. mirage-crypto-rng-lwt
  292. mirage-crypto-rng-mirage >= "0.8.8"
  293. mirage-device >= "2.0.0"
  294. mirage-dns < "3.0.0"
  295. mirage-entropy
  296. mirage-flow >= "1.0.3" & < "1.2.0" | >= "2.0.0"
  297. mirage-flow-combinators
  298. mirage-flow-lwt
  299. mirage-flow-rawlink
  300. mirage-flow-unix >= "1.3.0"
  301. mirage-fs >= "3.0.0"
  302. mirage-fs-lwt
  303. mirage-fs-unix < "1.1.1" | >= "1.3.0"
  304. mirage-http
  305. mirage-http-unix
  306. mirage-http-xen
  307. mirage-kv >= "3.0.0"
  308. mirage-kv-lwt
  309. mirage-kv-unix < "3.0.0"
  310. mirage-logs != "0.3.0"
  311. mirage-nat < "3.0.0"
  312. mirage-net >= "3.0.1"
  313. mirage-net-fd
  314. mirage-net-lwt
  315. mirage-net-macosx
  316. mirage-net-solo5
  317. mirage-net-unix >= "2.2.0"
  318. mirage-net-xen
  319. mirage-os-shim >= "3.0.0"
  320. mirage-profile
  321. mirage-protocols >= "4.0.0" & < "8.0.0"
  322. mirage-protocols-lwt
  323. mirage-qubes < "0.2" | >= "0.4" & < "0.9.4"
  324. mirage-qubes-ipv4 < "0.9.4"
  325. mirage-random-stdlib >= "0.1.0"
  326. mirage-runtime >= "3.7.0"
  327. mirage-solo5
  328. mirage-stack >= "2.0.0" & < "4.0.0"
  329. mirage-stack-lwt
  330. mirage-time >= "2.0.0"
  331. mirage-time-lwt
  332. mirage-time-unix
  333. mirage-types-lwt < "3.7.1"
  334. mirage-unix >= "3.0.0"
  335. mirage-vnetif
  336. mirage-vnetif-stack
  337. mirage-www >= "1.1.0"
  338. mirage-xen
  339. mirror
  340. monorobot
  341. moonpool-lwt
  342. mqtt = "0.0.2"
  343. mrmime >= "0.5.0"
  344. multipart-form-data >= "0.2.0"
  345. multipart_form >= "0.2.0" & < "0.4.0"
  346. multipart_form-lwt < "0.6.0"
  347. mwt
  348. naboris
  349. nanomsg
  350. nbd = "2.1.1" | >= "4.0.3"
  351. nbd-tool
  352. nbd-unix
  353. netchannel
  354. nocrypto >= "0.5.4"
  355. nottui-lwt
  356. nproc
  357. nsq >= "0.4.0"
  358. obrowser
  359. obuilder < "0.4"
  360. obus >= "1.2.1"
  361. ocaml-variants >= "4.00.1+mirage-unix" & < "4.00.1+open-types"
  362. ocluster < "0.2"
  363. ocluster-api < "0.2"
  364. ocplib-concur
  365. ocplib-resto
  366. ocsigen-start >= "4.1.0" & < "4.7.0"
  367. ocsigenserver >= "2.10"
  368. ocsipersist
  369. ocsipersist-dbm
  370. ocsipersist-lib
  371. ocsipersist-pgsql
  372. ocsipersist-sqlite
  373. odoc >= "2.0.0" & < "2.1.0"
  374. ojquery
  375. ojs-base < "0.6.0"
  376. opam-compiler < "0.2.0"
  377. opam-sync-github-prs
  378. openflow < "0.2.0"
  379. opium >= "0.11.0" & != "0.16.0" & < "0.19.0"
  380. opium_kernel
  381. opomodoro
  382. order-i3-xfce
  383. ordma >= "0.0.3"
  384. osc-lwt
  385. oskel >= "0.3.0"
  386. ounit-lwt < "2.2.0"
  387. ounit2-lwt
  388. owork
  389. ox < "1.1.0"
  390. paf
  391. paf-cohttp
  392. passage
  393. pcap-format >= "0.3.3" & < "0.5.0"
  394. pgx_lwt
  395. pgx_lwt_mirage
  396. pgx_lwt_unix < "2.0"
  397. plist-xml-lwt
  398. plotkicadsch >= "0.4.0"
  399. ppx_defer >= "0.4.0"
  400. ppx_deriving_rpc
  401. ppx_json_types
  402. ppx_netblob
  403. ppx_rapper_lwt
  404. ppx_sqlexpr
  405. prof_spacetime
  406. prometheus
  407. prometheus-app
  408. promise_jsoo_lwt
  409. protocol-9p >= "0.10.0"
  410. protocol-9p-unix
  411. qcow >= "0.8.1"
  412. qcow-format < "0.3"
  413. qcow-tool
  414. qfs = "0.5" | >= "0.7"
  415. quests
  416. rawlink >= "1.0" & < "2.1"
  417. rawlink-lwt
  418. redis-lwt
  419. resource-pooling >= "0.3.2"
  420. resp
  421. resp-mirage >= "0.10.0"
  422. resp-unix >= "0.10.0"
  423. resto
  424. resto-cohttp-client >= "0.4"
  425. resto-cohttp-self-serving-client
  426. resto-cohttp-server >= "0.4"
  427. resto-directory >= "0.4"
  428. riak
  429. ringo-lwt
  430. river
  431. rpc >= "1.5.1" & < "7.1.0"
  432. rpclib-js
  433. rpclib-lwt
  434. SZXX < "2.0.0"
  435. sanddb
  436. scgi
  437. sendmail-lwt
  438. sendmail-mirage
  439. serial
  440. session-cohttp-lwt
  441. session-cookie-lwt
  442. session-postgresql-lwt >= "0.4.1"
  443. sessions
  444. shared-block-ring < "2.3.0" | >= "3.0.0"
  445. shared-memory-ring >= "1.2.0" & < "2.0.0"
  446. shared-memory-ring-lwt
  447. slack
  448. slacko
  449. slipshow
  450. smtml >= "0.3.1"
  451. socket-daemon < "0.3.0"
  452. speed
  453. spin < "0.6.0"
  454. spotify-web-api < "0.2.1"
  455. sqlexpr = "0.7.1" | >= "0.9.0"
  456. statsd-client
  457. stog >= "0.16.0" & < "0.19.0"
  458. syndic >= "1.4" & < "1.6.0"
  459. tar-format >= "0.4.1"
  460. tar-mirage < "2.2.0"
  461. tar-unix < "3.0.0"
  462. tcpip >= "3.1.1" & < "3.4.1" | >= "4.1.0"
  463. teash
  464. telegraml
  465. tezos-error-monad >= "8.0" & < "9.0"
  466. tezos-lwt-result-stdlib >= "9.0" & < "11.0"
  467. tezos-p2p >= "11.0" & < "13.0"
  468. tezos-stdlib < "9.3"
  469. tezos-stdlib-unix < "9.0"
  470. tftp
  471. themoviedb
  472. timmy-lwt
  473. tls = "0.10.1" | >= "0.10.6" & < "0.16.0"
  474. tls-lwt < "0.17.4"
  475. tls-mirage
  476. transmission-rpc
  477. tube >= "4.3.0"
  478. tuntap >= "1.0.0" & < "1.7.0" | >= "2.0.0"
  479. twirp_cohttp_lwt_unix
  480. typerex-lldb
  481. u2f
  482. uring
  483. uspf
  484. uspf-lwt
  485. uspf-mirage
  486. utop >= "1.4.0"
  487. uwt >= "0.3.0"
  488. vchan >= "0.9.7" & < "2.0.0" | >= "2.0.3"
  489. vchan-unix
  490. vchan-xen
  491. vercel
  492. vhd-format >= "0.7.0" & < "0.8.0"
  493. vhd-format-lwt >= "0.12.0"
  494. vhd-tool < "0.12.0"
  495. vmnet >= "1.3.2"
  496. vpnkit >= "0.2.0"
  497. vue-jsoo < "0.3"
  498. webauthn
  499. websocket < "2.3"
  500. xe-unikernel-upload
  501. xen-api-client < "0.9.14"
  502. xen-block-driver
  503. xen-evtchn < "1.0.6" | >= "2.0.0"
  504. xen-evtchn-unix
  505. xen-gnt >= "2.2.3"
  506. xen-gnt-unix >= "4.0.2"
  507. xenctrl < "0.9.29" | >= "0.9.32"
  508. xenstore >= "1.3.0"
  509. xenstore_transport >= "1.0.0"
  510. xentropyd
  511. xlsx2csv
  512. yurt < "0.3"
  513. zarr-lwt
  514. zmq-lwt

Conflicts (1)

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

Innovation. Community. Security.