[erlang-questions] Problem running code from Armstrong's Programming Erlang
Kirill Zaborski
qrilka@REDACTED
Sat Jun 16 18:26:18 CEST 2007
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
Regards,
Kirill.
On 6/16/07, David Cabana <dcabana@REDACTED> wrote:
>
> I am reading a beta copy of Joe Armstrong's "Programming Erlang."
> The book's support site provides code samples for download; I have a
> question regarding a pair of similar bits of code from the site. One
> behaves as I would expect, the other locks up my erl session. I am
> trying to understand why the latter happens.
>
> Here is the first example, the one that works as expected. It is
> available at "http://media.pragprog.com/titles/jaerlang/code/
> area_server_final.erl". I have omitted some boilerplate comments at
> the beginning of the example files, for the sake of brevity.
>
> %% --------------- begin first example
> -module(area_server_final).
> -export([start/0, area/2]).
>
> start() -> spawn(fun loop/0).
>
> area(Pid, What) ->
> rpc(Pid, What).
>
> rpc(Pid, Request) ->
> Pid ! {self(), Request},
> receive
> {Pid, Response} ->
> Response
> end.
>
>
> loop() ->
> receive
> {From, {rectangle, Width, Ht}} ->
> From ! {self(), Width * Ht},
> loop();
> {From, {circle, R}} ->
> From ! {self(), 3.14159 * R * R},
> loop();
> {From, Other} ->
> From ! {self(), {error,Other}},
> loop()
> end.
> %% ---------------- end first example
>
> Here is the expected behavior, taken from an erl session:
> %% -------------- begin behavior, first example
>
> Eshell V5.5.4 (abort with ^G)
>
> 1>c(area_server_final).
> {ok,area_server_final}
>
> 2> Pid = area_server_final:start().
> <0.42.0>
>
> 3> area_server_final:area(Pid, {rectangle,2,3}).
> 6
>
> 4>
>
> %% -------------- end behavior, first example
> In particular, the area is computed and printed, and control returns
> to erl's read-eval-print loop.
>
> The second example is one that Mr Armstrong provides as a template
> for exploratory programming. I tried to use it as such, with no luck,
> and am trying to understand what is going wrong. The code is
> available at http://media.pragprog.com/titles/jaerlang/code/
> ctemplate.erl
> %% --------------- begin second example
>
> -module(ctemplate).
> -compile(export_all).
>
> start() ->
> spawn(fun() -> loop([]) end).
>
> rpc(Pid, Request) ->
> Pid ! {self(), Request},
> receive
> {Pid, Response} ->
> Response
> end.
>
> loop(X) ->
> receive
> Any ->
> io:format("Received:~p~n",[Any]),
> loop(X)
> end.
>
> %% --------------- end second example
>
> Notice that the second example is very similar to the first. It's
> purpose is to provide some scaffolding into which the user can
> interactively insert new messages and message handling code. When I
> try to use it, my session hangs. Here's the terminal session,
> starting with a new erl session.
>
> %% --------------- begin second example behavior
>
> Eshell V5.5.4 (abort with ^G)
> 1> c(ctemplate).
> {ok,ctemplate}
>
> 2> Pid = ctemplate:start().
> <0.38.0>
>
> 3> ctemplate:rpc(Pid, {foo}).
> Received:{<0.31.0>,{foo}}
>
> %% --------------- end second example behavior
>
> The erl session hangs at that point. I suspect user error, but have
> no clue what the error might be. I'm brand new to Erlang, and would
> much appreciate any insight on what's going wrong here. Thank you.
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20070616/ad9e29d5/attachment.htm>
More information about the erlang-questions
mailing list