# About Lwt: concurrency library

**URL:** <https://discuss.ocaml.org/t/about-lwt-concurrency-library/150>\
**Category:** Ecosystem\
**Tags:** lwt\
**Created:** [May 15, 2017, 7:09pm UTC](https://discuss.ocaml.org/t/about-lwt-concurrency-library/150 "2017-05-15T19:09:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![antron](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/antron/32/62_2.png) [@antron](https://discuss.ocaml.org/u/antron)\
**Post date:** [May 15, 2017, 7:09pm UTC](https://discuss.ocaml.org/t/about-lwt-concurrency-library/150/1 "2017-05-15T19:09:41Z")

</div>

[Lwt](https://github.com/ocsigen/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_](https://spring.io/understanding/javascript-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:

- Reply to this very topic, or open another one on [discuss.ocaml.org](https://discuss.ocaml.org)!
- [Open issues](https://github.com/ocsigen/lwt/issues/new) in the [Lwt repo](https://github.com/ocsigen/lwt).
- The [Lwt Gitter](https://gitter.im/ocaml-lwt/Lobby) has many frequent contributors lurking.
- Ask on [StackOverflow](http://stackoverflow.com/questions/ask?tags=ocaml,lwt,ocaml-lwt).
- The [#ocaml channel on freenode](http://webchat.freenode.net/?channels=#ocaml) is a good place to find experienced Lwt users.

Documentation:

- [The manual](https://ocsigen.org/lwt/manual/) can be found here. Sorry about its state – we are currently writing a new one!
- [Lwt tutorial](https://mirage.io/wiki/tutorial-lwt) from Mirage.
- [Concurrent Programming with Lwt](https://github.com/dkim/rwo-lwt#readme) by @dkim is a nice collection of examples, especially useful for those reading about concurrency in [Real World OCaml](https://realworldocaml.org/v1/en/html/concurrent-programming-with-async.html).
- [Simple Lwt server](http://www.baturin.org/code/lwt-counter-server/) by @dmbaturin.
- [A comparison of asynchronous programming in Lwt and Python](http://roscidus.com/blog/blog/2013/11/28/asynchronous-python-vs-ocaml/) by @talex5.

Announcements:

- Here, in the Lwt category of [discuss.ocaml.org](https://discuss.ocaml.org).
- In the [Lwt announcements issue](https://github.com/ocsigen/lwt/issues/309), which you can subscribe to.
- On the OCaml [mailing list](https://sympa.inria.fr/sympa/arc/caml-list).
- In [/r/ocaml](https://www.reddit.com/r/ocaml/).

If you’d like to follow development more closely, watch the [repo](https://github.com/ocsigen/lwt) 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](https://github.com/ocsigen/lwt/issues?q=is%3Aissue+is%3Aopen+label%3Aeasy).

Happy concurrent programming!

> **[ocsigen/lwt](https://github.com/ocsigen/lwt)**
>
> OCaml promises and concurrent I/O. Contribute to ocsigen/lwt development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![avsm](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/avsm/32/6_2.png) [@avsm](https://discuss.ocaml.org/u/avsm)\
**Post date:** [May 16, 2017, 3:46am UTC](https://discuss.ocaml.org/t/about-lwt-concurrency-library/150/2 "2017-05-16T03:46:57Z")

</div>



---

<div class="post-metadata">

**Author:** ![dmbaturin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dmbaturin/32/2074_2.png) [@dmbaturin](https://discuss.ocaml.org/u/dmbaturin)\
**Post date:** [May 30, 2017, 6:30pm UTC](https://discuss.ocaml.org/t/about-lwt-concurrency-library/150/3 "2017-05-30T18:30:28Z")

</div>

@talex5 also has a good post where he also compares Lwt with approaches typically used in Python: [Asynchronous Python vs OCaml](http://roscidus.com/blog/blog/2013/11/28/asynchronous-python-vs-ocaml/)

---

<div class="post-metadata">

**Author:** ![antron](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/antron/32/62_2.png) [@antron](https://discuss.ocaml.org/u/antron)\
**Post date:** [May 31, 2017, 5:24pm UTC](https://discuss.ocaml.org/t/about-lwt-concurrency-library/150/4 "2017-05-31T17:24:40Z")

</div>

@dmbaturin Thanks, added it to the top post.

---

<div class="post-metadata">

**Author:** ![alin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/alin/32/175_2.png) [@alin](https://discuss.ocaml.org/u/alin)\
**Post date:** [May 31, 2017, 11:05pm UTC](https://discuss.ocaml.org/t/about-lwt-concurrency-library/150/5 "2017-05-31T23:05:21Z")

</div>

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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![antron](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/antron/32/62_2.png) [@antron](https://discuss.ocaml.org/u/antron)\
**Post date:** [May 31, 2017, 11:51pm UTC](https://discuss.ocaml.org/t/about-lwt-concurrency-library/150/6 "2017-05-31T23:51:56Z")

</div>

@alin, try using this program as a sample:

```ocaml
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:

```auto
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`](https://ocsigen.org/lwt/3.0.0/api/Lwt_log_core) and [`Lwt_log`](https://ocsigen.org/lwt/3.0.0/api/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 `include`d 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 🙂

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](https://ocsigen.org/lwt/3.0.0/api/Lwt_log#VALrender)

---

<div class="post-metadata">

**Author:** ![alin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/alin/32/175_2.png) [@alin](https://discuss.ocaml.org/u/alin)\
**Post date:** [June 1, 2017, 12:05am UTC](https://discuss.ocaml.org/t/about-lwt-concurrency-library/150/7 "2017-06-01T00:05:29Z")

</div>

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 🙂

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

---

<div class="post-metadata">

**Author:** ![antron](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/antron/32/62_2.png) [@antron](https://discuss.ocaml.org/u/antron)\
**Post date:** [June 1, 2017, 2:17pm UTC](https://discuss.ocaml.org/t/about-lwt-concurrency-library/150/8 "2017-06-01T14:17:03Z")

</div>

Great 🙂

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](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…
