I haven't read the book so don't understand the purpose of those examples but it looks like in the second one the is no response message sent (e.g. From ! {self(), Width * Ht} in the first clause in area_server_final:loop/0) so receive blocks the shell. The workaround could be adding some timeout to the receive statement in ctemplate:rpc/2
<br><br>Regards,<br>Kirill.<br><br><div><span class="gmail_quote">On 6/16/07, <b class="gmail_sendername">David Cabana</b> <<a href="mailto:dcabana@nc.rr.com">dcabana@nc.rr.com</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I am reading a beta copy of Joe Armstrong's "Programming Erlang."<br>The book's support site provides code samples for download; I have a<br>question regarding a pair of similar bits of code from the site. One
<br>behaves as I would expect, the other locks up my erl session. I am<br>trying to understand why the latter happens.<br><br>Here is the first example, the one that works as expected. It is<br>available at "<a href="http://media.pragprog.com/titles/jaerlang/code/">
http://media.pragprog.com/titles/jaerlang/code/</a><br>area_server_final.erl".  I have omitted some boilerplate comments at<br>the beginning of the example files, for the sake of brevity.<br><br>%% ---------------  begin first example
<br>-module(area_server_final).<br>-export([start/0, area/2]).<br><br>start() -> spawn(fun loop/0).<br><br>area(Pid, What) -><br>     rpc(Pid, What).<br><br>rpc(Pid, Request) -><br>     Pid ! {self(), Request},<br>
     receive<br>        {Pid, Response} -><br>            Response<br>     end.<br><br><br>loop() -><br>     receive<br>        {From, {rectangle, Width, Ht}} -><br>            From ! {self(), Width * Ht},<br>            loop();
<br>        {From, {circle, R}} -><br>            From !  {self(), 3.14159 * R * R},<br>            loop();<br>        {From, Other} -><br>            From ! {self(), {error,Other}},<br>            loop()<br>     end.
<br>%% ---------------- end first example<br><br>Here is the expected behavior, taken from an erl session:<br>%% -------------- begin behavior, first example<br><br>Eshell V5.5.4  (abort with ^G)<br><br>1>c(area_server_final).
<br>{ok,area_server_final}<br><br>2> Pid = area_server_final:start().<br><0.42.0><br><br>3> area_server_final:area(Pid, {rectangle,2,3}).<br>6<br><br>4><br><br>%% -------------- end behavior, first example<br>
In particular, the area is computed and printed, and control returns<br>to erl's read-eval-print loop.<br><br>The second example is one that Mr Armstrong provides as a template<br>for exploratory programming. I tried to use it as such, with no luck,
<br>and am trying to understand what is going wrong. The code is<br>available at <a href="http://media.pragprog.com/titles/jaerlang/code/">http://media.pragprog.com/titles/jaerlang/code/</a><br>ctemplate.erl<br>%% --------------- begin second example
<br><br>-module(ctemplate).<br>-compile(export_all).<br><br>start() -><br>     spawn(fun() -> loop([]) end).<br><br>rpc(Pid, Request) -><br>     Pid ! {self(), Request},<br>     receive<br>        {Pid, Response} ->
<br>            Response<br>     end.<br><br>loop(X) -><br>     receive<br>        Any -><br>            io:format("Received:~p~n",[Any]),<br>            loop(X)<br>     end.<br><br>%% --------------- end second example
<br><br>Notice that the second example is very similar to the first. It's<br>purpose is to provide some scaffolding into which the user can<br>interactively insert new messages and message handling code.  When I<br>try to use it, my session hangs. Here's the terminal session,
<br>starting with a new erl session.<br><br>%% --------------- begin second example behavior<br><br>Eshell V5.5.4  (abort with ^G)<br>1> c(ctemplate).<br>{ok,ctemplate}<br><br>2> Pid = ctemplate:start().<br><0.38.0
><br><br>3> ctemplate:rpc(Pid, {foo}).<br>Received:{<0.31.0>,{foo}}<br><br>%% --------------- end second example behavior<br><br>The erl session hangs at that point. I suspect user error, but have<br>no clue what the error might be. I'm brand new to Erlang, and would
<br>much appreciate any insight on what's going wrong here. Thank you.<br><br>_______________________________________________<br>erlang-questions mailing list<br><a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org
</a><br><a href="http://www.erlang.org/mailman/listinfo/erlang-questions">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br></blockquote></div><br>