문제

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 !.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top