# Concurrent tasks in Lwt

**URL:** <https://discuss.ocaml.org/t/concurrent-tasks-in-lwt/14229>\
**Category:** Ecosystem\
**Tags:** lwt\
**Created:** [March 1, 2024, 7:17pm UTC](https://discuss.ocaml.org/t/concurrent-tasks-in-lwt/14229 "2024-03-01T19:17:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frederic\_Loyer](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/frederic_loyer/32/3472_2.png) [@Frederic\_Loyer](https://discuss.ocaml.org/u/Frederic_Loyer)\
**Post date:** [March 1, 2024, 7:17pm UTC](https://discuss.ocaml.org/t/concurrent-tasks-in-lwt/14229/1 "2024-03-01T19:17:35Z")

</div>

I have found that the easiest way to run multiple tasks concurrently on Lwt is:

```auto
Lwt_main.run @@ fun () ->
  ignore task1;
  ignore task2;
  ignore task3;
  Lwt.return ()

```

If `task1`, `task2` and `task3` are sequences of `Lwt` promises with long IO, sleep, pause, I can see the scheduler running them concurrently.

It can be simpler than:

```auto
  Lwt.both task1 @@ Lwt.both task2 task3

```

However, I wonder if it is an expected behaviour. I am contaminated with Haskell where an `ignore` doesn’t shortcut the monadic thread, but would really ignores its argument.

---

<div class="post-metadata">

**Author:** ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)\
**Post date:** [March 1, 2024, 7:20pm UTC](https://discuss.ocaml.org/t/concurrent-tasks-in-lwt/14229/2 "2024-03-01T19:20:36Z")

</div>

You’d want to at least know if any of the tasks fail, in which case you can use [Module Lwt](https://ocsigen.org/lwt/latest/api/Lwt#VALasync)

---

<div class="post-metadata">

**Author:** ![otini](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/otini/32/3533_2.png) [@otini](https://discuss.ocaml.org/u/otini)\
**Post date:** [March 3, 2024, 5:26am UTC](https://discuss.ocaml.org/t/concurrent-tasks-in-lwt/14229/3 "2024-03-03T05:26:08Z")

</div>

Isn’t

```ocaml
Lwt_main.run (Lwt.all [task1; task2; task3])

```

what you want to do in this case?

---

<div class="post-metadata">

**Author:** ![Frederic\_Loyer](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/frederic_loyer/32/3472_2.png) [@Frederic\_Loyer](https://discuss.ocaml.org/u/Frederic_Loyer)\
**Post date:** [March 3, 2024, 12:40pm UTC](https://discuss.ocaml.org/t/concurrent-tasks-in-lwt/14229/4 "2024-03-03T12:40:32Z")

</div>

Yes, this seems to be a better solution than nested `both`.

However, for a TCP server that would have to spawn promises, one for each connection, spawning them with `both` would most likely create a memory leak because of its `join` task. The `Lwt.on_failure` seems to fit the need, but we may have no usage of the exception handler.

---

<div class="post-metadata">

**Author:** ![raphael-proust](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/raphael-proust/32/415_2.png) [@raphael-proust](https://discuss.ocaml.org/u/raphael-proust)\
**Post date:** [March 4, 2024, 8:34am UTC](https://discuss.ocaml.org/t/concurrent-tasks-in-lwt/14229/5 "2024-03-04T08:34:26Z")

</div>

I don’t think that this is exactly your code. For one thing it doesn’t type because `Lwt_main.run` expects a promise. But more importantly, this will not do any work in the tasks that’s beyond I/O or pauses/yield. E.g., consider the code below

```auto
let () =
  Lwt_main.run @@
    let open Lwt.Syntax in
    ignore (let* () = Lwt_unix.sleep 5. in print_endline "waited 5"; Lwt.return ());
    ignore (let* () = Lwt_unix.sleep 7. in print_endline "waited 7"; Lwt.return ());
    Lwt.return ()

```

The executable exits immediately, no printing happens.

That’s because `Lwt_main.run` simply “awaits” for the given promise to resolve at which point it returns the value into the OCaml world and the execution of the program continues as normal (i.e., as a non-Lwt program would).

If there are pending I/O jobs and unresolved promises in the scheduler then they are simply ignored.

For a server, you can pass your main-loop to `run` (in the case of a server, the loop that calls accepts, spawns a task and then calls accept again, indefinitely). Or sometimes you can just pass a never-resolving promise (`fst (Lwt.task ())` or similar).

* * *

Regarding exception management, I’d recommend that you use `Lwt.dont_wait` rather than `on_failure`+`ignore`. It should be roughly equivalent but it will force you to have a `unit` promise and to wrap your promise inside a lambda which avoids exceptions leaking out.

E.g., the following will raise “outside of the promise” and the handler won’t be called

```auto
let p =
  if param < 0 then raise (Invalid_argument "");
  let* …
in
Lwt.on_failure p (fun exn -> …);
ignore p;

```

whereas the same code with `dont_wait` will pass the exception to the handler.
