Yes, your code is correct. The principle of lwt supported is that anything that could potentially take 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 fl, iter_s will call f for each element of l, waiting for completion between each element. In contrast, in iter_p fl, iter_p will call f on all elements of l, and then wait for all threads to complete.
On non-blocking functions, the principle of luminous flux is that they continue to work until they reach the "point of cooperation", i.e. points where the thread can be safely interrupted or has nothing to do, as in sleep .
But you need to declare that you enter a “collaboration point” before doing sleep . That's why the whole Unix library was wrapped, so when you want to perform a time-consuming operation (like a write ), a collaboration point is automatically reached.
For your own function, if you use I / O from Unix, you should use the Lwt version instead ( Lwt_unix.sleep instead of Unix.sleep )
source share