<br><br><div class="gmail_quote">On Wed, Mar 11, 2009 at 11:31 PM, Allen McPherson <span dir="ltr"><<a href="mailto:mcpherson@lanl.gov">mcpherson@lanl.gov</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Here's a snippet of code that I'm using:<br>
<br>
%%  Once nodes have been started we launch the code to execute the<br>
%%  benchmarks.<br>
launch(L) -><br>
     launch(L, []).<br>
launch([], Pids) -><br>
     Pids;<br>
launch([H|T], Pids) -><br>
     S = self(),             %  loop(self(), start) would evaluate<br>
self() inside loop() function<br>
     launch(T, lists:append(Pids, [spawn(H, fun() -> loop(S, start)<br>
end)])).<br>
<br>
%%  Main benchmark loop.  Receives request for particular type of test.<br>
%%  Spawns code to handle benchmark on its local node.<br>
loop(MasterPid, Mode) -><br>
     ...<br>
<br>
<br>
The line in question is:<br>
            S = self(),<br>
----->     launch(T, lists:append(Pids, [spawn(H, fun() -> loop(S,<br>
start) end)])).<br>
<br>
I find that if I use:<br>
<br>
            launch(T, lists:append(Pids, [spawn(H, fun() -><br>
loop(self(), start) end)])).</blockquote><div><br>You can read fun() -> loop(self(), start) end as make function which do  loop(self(), start) and evaluate it in new process. It means self() is evaluated in this process not in master process. It not means that self() is evaluated in loop/2 but outside it even inside it's process which cause trouble.<br>
<br>You can rewrite it using list comprehension<br><br>launch(L)-><br>    S=self(),<br>    [ spawn(N, fun()->loop(S, start) end) || N<-L ].<br><br>or in tail call manner (lists:append/2 or ++ is not often used especially in loop where first parameter is growing)<br>
<br>launch(L) -> launch(L, []).<br><br>launch([], Pids)->lists:reverse(Pids); % or just Pids if doesn't care Pids order<br>launch([H|T], Pids)-><br>  S=self(),<br>  launch(T, [pawn(N, fun()->loop(S, start) end)|Pids]).<br>
<br>It's common idiom in Erlang ot avoid lists:append/2 misuse.<br><br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br>
<br>
instead, self() is evaluated everywhere in loop() that MasterPid occurs.<br>
Intuitively I sort of get it, by formally I don't really understand<br>
what's going on here...why the delayed evaluation? I would think it<br>
would be<br>
evaluate before the spawn, but it's getting evaluated after the spawn<br>
on node<br>
'H'.<br>
<br>
Probably another dumb question, but can someone shed some light this.<br>
It seems like a fundamental concept to understand.<br>
<font color="#888888"><br>
--<br>
Al<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" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>
</font></blockquote></div><br><br clear="all"><br>-- <br>--Hynek (Pichi) Vychodil<br><br>Analyze your data in minutes. Share your insights instantly. Thrill your boss.  Be a data hero!<br>Try Good Data now for free: <a href="http://www.gooddata.com">www.gooddata.com</a><br>