package lwt_ppx

Build not checked
  1. Overview
  2. Docs
PPX syntax for Lwt, providing something similar to async/await from JavaScript

Install

dune-project
 Dependency

Authors

Maintainers

Sources

6.0.0-beta01.tar.gz
md5=6bb978c590433a218a53722b985a01d4
sha512=529c8c920716cf204efd60a2bad3f817eed6eed120a8e8a8dae763ab7f4c45799a74004d0d0a2b8ec08f37c81b77f1a4813426a25f256b254dfa846c86debe44

Description

Added to opam-repository:

README

Lwt

version GitHub Actions status

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* ... 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 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 (4)

  1. lwt = version
  2. ppxlib >= "0.36"
  3. ocaml >= "4.08"
  4. dune >= "3.15"

Dev Dependencies (1)

  1. odoc with-doc

Optional Dependencies

None

  1. activitypub
  2. anthill
  3. awso-lwt
  4. azure-cosmos-db < "0.4.0"
  5. azure-cosmos-db-lwt
  6. bitflyer
  7. canary
  8. chamo
  9. chess_com_api
  10. cohttp-lwt-jsoo >= "5.3.0"
  11. cosmetyc_native
  12. css
  13. curl_lwt
  14. dap
  15. devkit
  16. docker_hub < "0.2.0"
  17. dream >= "1.0.0~alpha4"
  18. dream-encoding >= "0.2.0"
  19. dream-httpaf
  20. dream-inertia
  21. dream-livereload >= "0.2.0"
  22. dream-mirage
  23. dream-pure
  24. dream-serve
  25. dream_middleware_ext
  26. earlybird
  27. elasticsearch-cli
  28. eliom < "12.0.0"
  29. erssical
  30. FPauth-core
  31. gdbprofiler
  32. gmocoin
  33. granary
  34. granary_repl
  35. gremlin
  36. guardian
  37. hyper
  38. i3ipc
  39. jupyter
  40. jupyter-kernel < "0.5"
  41. lambdapi >= "2.4.0"
  42. ldp
  43. lichess_api
  44. lru_cache
  45. mehari-lwt-unix < "0.3"
  46. mehari-mirage < "0.3"
  47. menhir-lsp
  48. menhirformat >= "0.3.0"
  49. monorobot
  50. mpris
  51. mqtt
  52. multipart-form-data
  53. mwt
  54. naboris >= "0.1.3"
  55. neo4j_bolt
  56. obus >= "1.2.1"
  57. ocaml-ai-sdk
  58. ocsigen-toolkit >= "4.3.0"
  59. ocsipersist-lib >= "1.0.3" & < "2.1.0"
  60. ojs-base
  61. ojs_base
  62. opam-check-npm-deps < "4.1.0"
  63. openai
  64. opentelemetry-client-cohttp-eio < "0.90"
  65. opentelemetry-client-cohttp-lwt
  66. opentelemetry-client-ocurl-lwt
  67. opentelemetry-lwt >= "0.5"
  68. oraft >= "0.3.0"
  69. order-i3-xfce
  70. owork
  71. passage < "0.1.8"
  72. pgn_parser
  73. plotkicadsch
  74. ppx_cstruct >= "6.0.1"
  75. ppx_defer >= "0.4.0"
  76. qcow-stream >= "0.13.0"
  77. qcow-types >= "0.13.0"
  78. qdrant < "0.2.0"
  79. qfs
  80. quests
  81. quickterface
  82. rdf_json_ld
  83. rdf_lwt
  84. SZXX < "2.0.0"
  85. scgi
  86. server-reason-react
  87. sihl >= "0.6.0~rc1"
  88. slack
  89. slacko
  90. speed
  91. statocaml
  92. stk
  93. stog >= "0.19.0"
  94. syguslib-utils
  95. testcontainers
  96. testcontainers-elasticsearch
  97. testcontainers-kafka
  98. testcontainers-localstack
  99. testcontainers-memcached
  100. testcontainers-mockserver
  101. testcontainers-mongo
  102. testcontainers-mysql
  103. testcontainers-postgres
  104. testcontainers-rabbitmq
  105. testcontainers-redis
  106. textrazor
  107. tidy_email_mailgun
  108. tidy_email_sendgrid
  109. tidy_email_smtp
  110. usb
  111. ws-server

Conflicts

None

Build status

Results of building lwt_ppx with each compiler on check.ci.ocaml.org, run of against opam-repository 8a77e58. All results for this package.

Each result links to its build log. To keep crawlers from overloading the site, build logs require a GitHub sign-in (see ocurrent/opam-health-check). On your first log, GitHub asks you to “Authorize opam-health-check”, the OCurrent app that runs these checks; it sees only your public profile, not your repositories. The sign-in lasts 30 days.

This version is not in the latest run; the versions below are.

Version OCaml 4.11 OCaml 4.14 OCaml 5.2 OCaml 5.4 OCaml 5.5
6.1.0 n/a good good good good
5.9.3 good good good good good
  • good Package successfully built
  • partial One of the dependencies failed to build
  • bad Package failed to build
  • n/a Package is not available in this environment
  • internal Internal failure