package lwt

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

Install

Dune Dependency

Authors

Maintainers

Sources

5.1.1.tar.gz
sha256=078a6c351d94d6eeb827c62a25c87ac11715c791f4b642d85303740c2cac8d3f
md5=4ddec0f42b7aa4a310175a14c47c60a3

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

Conflicts (1)

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

Innovation. Community. Security.