package lwt

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

Install

Dune Dependency

Authors

Maintainers

Sources

4.2.0.tar.gz
sha256=6d679ec2f33897675066d4c58c1d57d84045727263c16279b23c3a9ce13fb3f0
md5=2ce7827948adc611319f9449e4519070

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: 25 Mar 2019

README

Lwt   

Lwt is OCaml's concurrent programming library. 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 -package 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 Gitter chat, ask in #ocaml, on discuss.ocaml.org, or on Stack Overflow. Please do ask! Even apparently simple questions often end up educating other users, not to mention enlightening the maintainers!

Release announcements are made in /r/ocaml, and on discuss.ocaml.org. Watching the repo for "Releases only" is also an option.


Contributing

  • We maintain easy starter issues. These are thoroughly explained and hyperlinked. We hope that this makes working on Lwt accessible even to relative OCaml beginners :)

  • 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.

  • The overall development plan can be found in the roadmap.

  • Ask us anything, whether it's about working on Lwt, or any question at all about it :)

  • The documentation always needs proofreading and fixes.

  • Despite a lot of progress, Lwt still needs more tests.

  • 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 (6)

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

Conflicts (1)

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

Innovation. Community. Security.