Yes, your code is correct. The principle of lwt supported
is that everything that can potentially takes time in your code should return an Lwt value.
About Lwt_list.iter, you can choose whether you want the treatment to be parallel or sequential, by choosing between iter_p
and iter_s
:
In iter_s f l, iter_s will call f on each elements of l, waiting for completion between each element. On the contrary, in iter_p f l, iter_p will call f on all elements of l, then wait for all the threads to terminate.
About the non-blocking functions, the principle of the Light Weight Threads is that they keep running until they reach a "cooperation point", i.e. a point where the thread can be safely interrupted or has nothing to do, like in a sleep
.
But you have to declare you enter a "cooperation point" before actually doing the sleep
. This is why the whole Unix library has been wrapped, so that when you want to do an operation that takes time (e.g. a write
), a cooperation point is automatically reached.
For your own function, if you use IOs operations from Unix, you should instead use the Lwt version (Lwt_unix.sleep
instead of Unix.sleep
)