Question

It seems that the functionality of gen_server:handle_cast/3 and gen_server:handle_info/3 is very similar. I know that handle_info/3 are used to handle timeout events.

Is there any other rule to use them? In my opinion, I prefer to use handle_info/3 to handle async messages. As it is shorter to use the grammer sugar !.

Was it helpful?

Solution

gen_server is an abstraction over a typical process message loop. It provides you with API like call and cast to send messages to the process BUT as you also have the PID of the process, any code can send message to the process without going through the call or cast APIs and in those cases the handle_info callback is called. If you want to send messages to your process using the ! operator then you probably don't need gen_server OR if you want to use gen_server then it is advised to use call and cast APIs for your server functionality and handle_info for other kind of messages.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top