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   

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

Conflicts (1)

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

Innovation. Community. Security.