[erlang-questions] gen_server & wait/notify

zxq9 <>
Fri Sep 29 09:23:42 CEST 2017


On 2017年09月28日 木曜日 17:58:08 Chris Waymire wrote:
> handle_call({request, Data#data{uid=UID}}, _From, State) ->
>     make_asycnc_req(Data),
>     receive
>         {UID, Response} -> {reply, Response, State}
>     end.
> 
> handle_info({response, Response#response{uid=UID}}, State) ->
>     self() ! {UID, Response},
>     {noreply, State}.


Your naked `receive` will work just fine. The flow of the process has never
returned to the gen_server module yet to await another message to dispatch,
so when you write a naked `receive` in some handling code that is exactly
where the process will block, and you can receive any arbitrary thing you
want there. Just be careful not to match on any system or gen_server message
types and you'll get the behavior you expect (though your mailbox may be
filling up with other stuff in the meantime).

-Craig


More information about the erlang-questions mailing list