package lwt_ppx
Install
    
    dune-project
 Dependency
Authors
Maintainers
Sources
md5=94272fac89c5bf21a89c102b8a8f35a5
    
    
  sha512=8951b94555e930634375816d71815b9d85daad6ffb7dab24864661504d11be26575ab0b237196c54693efa372a9b69cdc1d5068a20a250dc0bbb4a3c03c5fda1
    
    
  Description
Published: 26 Nov 2021
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* ... 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 Lwtprovides promises...
- ...and a few pure-OCaml helpers, such as promise-friendly mutexes, condition variables, and mvars.
- There is a big Unix binding, Lwt_unixthat binds almost every Unix system call. A higher-level moduleLwt_ioprovides nice I/O channels.
- Lwt_processis for subprocess handling.
- Lwt_preemptivespawns system threads.
- The PPX syntax allows using all of the above without going crazy!
- There are also some other helpers, such as Lwt_reactfor reactive programming. See the table of contents on the linked manual pages!
Installing
- Use your system package manager to install a development libev package. It is often called libev-devorlibev-devel.
- 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.mdcontains 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
- alcotest — unit testing
- angstrom — parser combinators
- cohttp — HTTP client and server
- cstruct — interop with C-like structures
- ezjsonm — JSON parsing and output
- faraday — serialization combinators
- logs — logging
- lwt-parallel — distributed computing
- mwt — preemptive (system) thread pools
- opium — web framework
Dev Dependencies
None
Used by (77)
- activitypub
- anthill
- azure-cosmos-db
- canary
- chamo
- chess_com_api
- 
  
    cohttp-lwt-jsoo
  
  
    >= "5.3.0"
- css
- curl_lwt
- dap
- devkit
- 
  
    docker_hub
  
  
    < "0.2.0"
- dream
- 
  
    dream-encoding
  
  
    >= "0.2.0"
- dream-httpaf
- dream-inertia
- 
  
    dream-livereload
  
  
    >= "0.2.0"
- dream-pure
- dream-serve
- dream_middleware_ext
- earlybird
- elasticsearch-cli
- eliom
- erssical
- FPauth-core
- gdbprofiler
- gremlin
- hyper
- i3ipc
- jupyter
- 
  
    jupyter-kernel
  
  
    < "0.5"
- 
  
    lambdapi
  
  
    >= "2.4.0"
- ldp
- lichess_api
- lru_cache
- monorobot
- mpris
- multipart-form-data
- mwt
- 
  
    naboris
  
  
    >= "0.1.3"
- noise
- 
  
    obus
  
  
    >= "1.2.0"
- 
  
    ocsipersist-lib
  
  
    >= "1.0.3"
- ojs-base
- ojs_base
- opam-check-npm-deps
- openai
- opentelemetry-client-cohttp-eio
- opentelemetry-client-cohttp-lwt
- 
  
    opentelemetry-lwt
  
  
    >= "0.5"
- 
  
    oraft
  
  
    >= "0.3.0"
- order-i3-xfce
- owork
- 
  
    passage
  
  
    < "0.1.8"
- pgn_parser
- plotkicadsch
- 
  
    ppx_cstruct
  
  
    >= "6.0.1"
- 
  
    ppx_defer
  
  
    >= "0.4.0"
- 
  
    qfs
  
  
    >= "0.7"
- quests
- 
  
    rdf_json_ld
  
  
    < "1.0.0"
- 
  
    rdf_lwt
  
  
    < "1.0.0"
- 
  
    SZXX
  
  
    < "2.0.0"
- scgi
- 
  
    sihl
  
  
    >= "0.6.0~rc1"
- slack
- slacko
- speed
- stk
- 
  
    stog
  
  
    >= "0.19.0" & < "1.0.0"
- syguslib-utils
- textrazor
- tidy_email_mailgun
- tidy_email_sendgrid
- tidy_email_smtp
- usb
- ws-server
Conflicts
None