<div dir="ltr"><div>Hi!</div><div><br></div><div>If you want your gen_server to be able to service other requests during the async request, then you want something like below (modulo error handling and typos and bugs)</div><div><br></div><div>best regards,</div><div>Vlad</div><div><br></div><div>handle_call({request, Data#data{uid=UID}}, From, State) -><br></div><div> make_async_req(Data),</div><div> NewState = State#state{pending=[{UID, From}|State#state.pending]}</div><div> {noreply, NewState}.<br></div><div><br></div><div>handle_info({response, Response#response{uid=UID}}, State=#state{pending=Pending}) -></div><div> {value, {UID, From}, NewPending} = lists:keytake(UID, 1, Pending),</div><div> gen_server:reply(From, Response),</div><div> NewState = State#state{pending=NewPending},</div><div> {noreply, NewState}.</div><div><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Sep 29, 2017 at 2:58 AM, Chris Waymire <span dir="ltr"><<a href="mailto:chris@waymire.net" target="_blank">chris@waymire.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">I need some help finding a pattern/solution to a problem that is proving more difficult then it should, it seems.<div><br></div><div>I have a gen_server module that needs to take an asynchronous request/response operation and expose it as a synchronous operation. Typically I would use receive to block and have the asynchronous operation issue a message to unblock it but that won't work (i believe) with a gen_server since it loops over receive and forwards them to calls to handle_info. The code below demonstrates the core of the matter of what i'm talking about. I'm not sure what the solution would be to achieve this functionality while using the gen_server behaviour.</div><div><br></div><div><br></div><div><br></div><div>handle_call({request, Data#data{uid=UID}}, _From, State) -><br></div><div> make_asycnc_req(Data),</div><div> receive<br></div><div> {UID, Response} -> {reply, Response, State}</div><div> end.</div><div><br></div><div>handle_info({response, Response#response{uid=UID}}, State) -></div><div> self() ! {UID, Response},</div><div> {noreply, State}.</div><div><br></div><div>Thanks,</div><div><br></div><div>-- Chris</div></div>
<br>______________________________<wbr>_________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/<wbr>listinfo/erlang-questions</a><br>
<br></blockquote></div><br></div></div>