Question

This is the code I have:

handle_cast(start, #state{started = false} = State) ->
    gen_server:cast(self(), add_process),
    {noreply, State#state{started = true}};
handle_cast(add_process, State) -> ...

Is it ok to call gen_server:cast from insinde the handle_cast function? What I expect it to do is that handle_cast will return a new state and then the add_process message is processed immediately.

Was it helpful?

Solution

That is correct, except that the message might not be processed "immediately" - it is put on the message queue, and the messages before it in the queue will be processed first.

As you've probably figured out already, this applies to gen_server:cast and messages sent using !, but not to gen_server:call, which would cause a deadlock here.

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