I am working on a simple introduction talk to OTP. One of the things I myself am wondering about, however, is when Agent
is useful and when Task.Supervisor
is useful, as until now I’ve never felt the need to use them. So I am kind of searching for use-cases, so I can explain why they exist and when you should use them.
Agent
In the projects I have made in the past, I always wrapped a GenServer myself, instead of relying on Agent, because I knew that the message-passing and state-handling would become a little more complicated in the future. But I am not sure if this is the correct approach.
When is it better to use an Agent, and when is it better to just build a GenServer from scratch?
Task.Supervisor
I have used a lot of Tasks to enable small pieces of code that do not depend on each other to run concurrently. However, I’ve never had the need to supervise them, because they are either linked to the process that started them when using Task.async
(so if a task fails, that process and all other tasks fail), or they were run with Task.start
when I did not care about the return result, and therefore also did not care about if it succeeded or not.
In what cases is supervising tasks useful?