package lwt

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

Install

Dune Dependency

Authors

Maintainers

Sources

5.3.0.tar.gz
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   

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

Conflicts (1)

  1. ocaml-variants = "4.02.1+BER"