Using the send operator is very common. To avoid endless waiting in a receive statement you can use after:<br> <br>receive<br>
{Server, Response} -><br>
?DUMP({send_and_receive, received, Response}),<br>
Response<br> after 1000 -><br> void<br>
end.<br><br><br><div class="gmail_quote">On Tue, Dec 9, 2008 at 5:30 AM, Jack Orenstein <span dir="ltr"><<a href="mailto:jao@geophile.com">jao@geophile.com</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;">
Please excuse a very basic question.<br>
<br>
How can an Erlang process avoid hanging when sending a message to a<br>
process that no longer exists, (as in the code below)? I understand<br>
that using the send operator (!) directly is pretty uncommon, and<br>
that applications would normally use OTP, but how does OTP deal with<br>
the problem?<br>
<br>
Jack<br>
<br>
<br>
-module(test).<br>
<br>
-export([main/0]).<br>
<br>
-define(DUMP(X), io:format("~p:~p - ~p = ~p~n", [?MODULE, ?LINE, ??X,<br>
X])).<br>
<br>
main() -><br>
P = spawn(fun() -> loop() end),<br>
send_and_receive(P, hello),<br>
send(P, exit),<br>
send_and_receive(P, hello).<br>
<br>
send(Server, Request) -><br>
?DUMP({send, Request}),<br>
Server ! {self(), Request}.<br>
<br>
send_and_receive(Server, Request) -><br>
?DUMP({send_and_receive, send, Request}),<br>
Server ! {self(), Request},<br>
receive<br>
{Server, Response} -><br>
?DUMP({send_and_receive, received, Response}),<br>
Response<br>
end.<br>
<br>
loop() -><br>
receive<br>
{From, hello} -><br>
?DUMP(hello),<br>
From ! {self(), hi},<br>
loop();<br>
{_From, exit} -><br>
?DUMP(exit),<br>
void<br>
end.<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" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>
</blockquote></div><br>