When to use handle_info and handle_cast

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

Is there any other rule to use them? In my opinion, I prefer to use handle_info/3for processing asynchronous messages. As the use of grammar is shorter than sugar !.

+4
source share
1 answer

gen_server- abstraction of a typical process message loop. It provides you with an API, for example call, castfor sending messages to a process, but since you also have a PIDprocess, any code can send a message to the process without passing through calleither the castAPI, and in these cases a callback is called handle_info. If you want to send messages to your process by an operator !, you probably do not need gen_serveror if you want to use gen_server, then it is recommended to use the API calland castfor your server and handle_infoother messages.

+6
source

Source: https://habr.com/ru/post/1540193/


All Articles