package lwt

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

Install

Dune Dependency

Authors

Maintainers

Sources

4.2.1.tar.gz
sha256=6663be42156c7224c4d2c57842e669d99895dbeb87ebeb46deb90c5a0f0830c1
md5=9d648386ca0a9978eb9487de36b781cc

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

Conflicts (1)

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

Innovation. Community. Security.