About Lwt: concurrency library

Lwt is a library for concurrent programming in OCaml. It is developed by the community, and is one of OCaml’s most-used libraries.

Lwt is based around the familiar concept of promises. When you make an I/O request with Lwt, the library gives you a promise. It then runs the request in the background, without blocking the rest of your program. When the request completes, Lwt fulfills the promise, running any handlers you’ve attached. Lwt can process huge numbers of such requests concurrently.

In the meantime, your code (e.g. the handlers) runs in one thread. This eliminates the need to worry about race conditions, set up locking, or other such complications. You can still spawn threads on an opt-in basis, however.

Lwt is routinely used in high-concurrency, high-load applications.

Lwt resources and community

Lwt welcomes users and contributors of all levels of experience!

Please ask any questions you may have. You’d be surprised at how often simple questions help other users, and educate the maintainers. And, if you want to take the plunge and help Lwt and the broader OCaml community, we’d love to help you get gently started as a contributor.

To reach us:

Documentation:

Announcements:

If you’d like to follow development more closely, watch the repo on GitHub

And, if you are looking for something helpful, but not too challenging to get started contributing with, we maintain a list of easy issues.

Happy concurrent programming!

9 Likes

@talex5 also has a good post where he also compares Lwt with approaches typically used in Python: Asynchronous Python vs OCaml

3 Likes

@dmbaturin Thanks, added it to the top post.

I was really trying to find some more complex examples on how to use Lwt_log, but I wasn’t able to find anything considerable; what I wanted to do, was to simply display some log messages to stdout, and that messages to have at least the timestamp, and the logging level; I’m pretty new to the OCaml world, and I wasn’t able to figure out from the Lwt_log’s documentation how to do that. The only thing I was able to do, was to display some logs by doing:

Lwt_log.add_rule "*" Lwt_log.Info;;
Lwt_log.info "blah";;

But the logging messages look pretty basic, and that’s where I got stuck.
Any tips on this thing, please?

Thanks.

2 Likes

@alin, try using this program as a sample:

let () =
  Lwt_log_core.default :=
    Lwt_log.channel
      ~template:"$(date).$(milliseconds) [$(level)] $(message)"
      ~close_mode:`Keep
      ~channel:Lwt_io.stdout
      ();

  Lwt_log_core.add_rule "*" Lwt_log_core.Info;

  Lwt_main.run begin
    Lwt_log_core.info "blah"
  end

(* ocamlfind opt -linkpkg -package lwt.unix foo.ml && ./a.out *)

It produces output like this:

May 31 18:49:30.863 [info] blah

The Lwt_log docs are admittedly confusing, as they are split between two modules: Lwt_log_core and Lwt_log. This also got me once, because I last used Lwt_log heavily before it was split up like this.

In the program, I deliberately used Lwt_log_core for things that come from that module, so you can quickly find out what’s documented there, and what’s documented in Lwt_log. Lwt_log_core is actually included in Lwt_log, however, so normally one would just use Lwt_log to qualify all the identifiers.

EDIT: and thanks for asking. Another useful reminder to improve these docs :slight_smile:

EDIT 2: I also want to add that the template can be more elaborate when you are using a logger from Lwt_log, as you can see in the Lwt_log docs:

https://ocsigen.org/lwt/3.0.0/api/Lwt_log#VALrender

3 Likes

Nice; this was exactly what I was looking for; thanks so much.
I was able to see that the Lwt_log_core gets mixed in within Lwt_log, but the thing I was missing was example on a “normal” logging workflow (i.e. how to configure it, how to use it, etc.).
Btw, even though the Lwt documentation is incomplete here and there, I find it very useful and well organised; good job with that so far :slight_smile:

EDIT 1: a bit late now, but reading this code helps https://github.com/ocsigen/lwt/blob/master/src/unix/lwt_log.ml understanding what’s happening there

2 Likes

Great :slight_smile:

We’re actually going to rewrite all the docs (or just write them, for the many parts that are missing): https://github.com/ocsigen/lwt/projects/2. As you could probably guess from the number of items in the project, it’s going to take a while…

1 Like