• Stars
    star
    710
  • Rank 63,751 (Top 2 %)
  • Language
    OCaml
  • License
    MIT License
  • Created over 11 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

OCaml promises and concurrent I/O

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

  • 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

More Repositories

1

js_of_ocaml

Compiler from OCaml to Javascript.
OCaml
950
star
2

eliom

Multi-tier framework for programming web and mobile applications in OCaml.
OCaml
301
star
3

ts2ocaml

Generate OCaml bindings from TypeScript definitions via the TypeScript compiler API
F#
172
star
4

tyxml

Build valid HTML and SVG documents
OCaml
166
star
5

ocsigenserver

Web server in OCaml.
OCaml
101
star
6

ocsigen-start

Ocsigen-start: Higher-level library to develop Web and mobile applications with users, (pre)registration, notifications, etc.
OCaml
75
star
7

reactiveData

Functional reactive programming with incremental changes in data structures
OCaml
52
star
8

macaque

OCaml
38
star
9

ocsigen-toolkit

User interface widgets for OCaml applications
OCaml
32
star
10

ts2ocaml-example

Example use of ts2ocaml
Makefile
18
star
11

obrowser

OCaml virtual machine written in Javascript.
OCaml
14
star
12

ojquery

jQuery Binding for Eliom.
OCaml
11
star
13

lwt_glib

GLib/GTK event loop for Lwt
OCaml
11
star
14

tutorial-deprecated

Deprecated. Please use repository tuto instead.
OCaml
10
star
15

tuto

Ocsigen tutorial
OCaml
10
star
16

graffiti

A tiny collaborative drawing application in eliom.
OCaml
10
star
17

ocaml-eliom

A modified OCaml compiler for eliom programs
OCaml
10
star
18

lwt_ssl

OpenSSL binding with concurrent I/O
OCaml
9
star
19

ocaml-indexeddb

OCaml binding for IndexedDB
OCaml
9
star
20

eliomlang

Runtime and tools for the eliom language
OCaml
9
star
21

resource-pooling

a derivative of Lwt_pool with extensions
OCaml
8
star
22

ocsimore

A wiki in Eliom.
OCaml
7
star
23

html_of_wiki

OCaml
6
star
24

ocsipersist

OCaml
6
star
25

wikidoc

Convert OCaml documentation to wiki.
OCaml
6
star
26

ocsipersist-legacy

An extension for ocsigen server to use a key/value store
OCaml
6
star
27

ocsigen.github.io

Ocsigen Web site
HTML
6
star
28

eliom-widgets

Widgets for client-server Web applications written with Eliom
OCaml
6
star
29

ocsigen-ppx-rpc

This PPX adds a syntax for RPCs for Eliom and Ocsigen Start
OCaml
6
star
30

lwt_domain

Parallelism in Lwt using domainslib
OCaml
5
star
31

ojwidgets

Widgets for browser applications written in OCaml (also see Eliom widgets)
OCaml
5
star
32

lwt_log

(Deprecated) Lwt-friendly logger
OCaml
4
star
33

old.ocsigen.org

The old http://ocsigen.org/ website.
OCaml
4
star
34

oclosure

Binding of the Google Closure Javascript library in OCaml using Js_of_ocaml.
JavaScript
4
star
35

ocsigen-widgets

Widgets for Web apps in OCaml (client only or client-server)
OCaml
3
star
36

ocsforge

Ocsforge is a project aiming at creating sites for project hosting (forges).
OCaml
2
star
37

docker-buildbot

Shell
2
star
38

ocsigen.org-data-deprecated

Ocsigen.org versioned data. DEPRECATED. The doc is now in each project (branch wikidoc)
JavaScript
2
star
39

.github

1
star
40

js_of_ocaml-ocamlbuild

OCaml
1
star
41

lwt_camlp4

(Deprecated) Camlp4 syntax for Lwt
OCaml
1
star
42

ocamlweb-roadmap

Roadmap for better Web and mobile developement tools in OCaml
1
star
43

pa_include

Include .mli as module signature.
OCaml
1
star
44

docker-ocsigen

A base Docker image for Ocsigen (with opam, eliom, js_of_ocaml and ocsigenserver installed)
Shell
1
star
45

eliomlang-playground

A (perpetually broken) playground for a minimal webapp with eliomlang
Makefile
1
star
46

ocsigen.org-deprecated

ocsigen.org site before 2019 (wiki format) DEPRECATED. Use https://github.com/ocsigen/ocsigen.github.io instead.
1
star
47

quickstart-guide

HTML
1
star