[erlang-questions] Sending a message to a process that no longer exists

Rapsey rapsey@REDACTED
Tue Dec 9 06:58:17 CET 2008


Using the send operator is very common. To avoid endless waiting in a
receive statement you can use after:

receive
        {Server, Response} ->
            ?DUMP({send_and_receive, received, Response}),
            Response
       after 1000 ->
             void
  end.


On Tue, Dec 9, 2008 at 5:30 AM, Jack Orenstein <jao@REDACTED> wrote:

> Please excuse a very basic question.
>
> How can an Erlang process avoid hanging when sending a message to a
> process that no longer exists, (as in the code below)? I understand
> that using the send operator (!) directly is pretty uncommon, and
> that applications would normally use OTP, but how does OTP deal with
> the problem?
>
> Jack
>
>
> -module(test).
>
> -export([main/0]).
>
> -define(DUMP(X), io:format("~p:~p - ~p = ~p~n", [?MODULE, ?LINE, ??X,
> X])).
>
> main() ->
>     P = spawn(fun() -> loop() end),
>     send_and_receive(P, hello),
>     send(P, exit),
>     send_and_receive(P, hello).
>
> send(Server, Request) ->
>     ?DUMP({send, Request}),
>     Server ! {self(), Request}.
>
> send_and_receive(Server, Request) ->
>     ?DUMP({send_and_receive, send, Request}),
>     Server ! {self(), Request},
>     receive
>         {Server, Response} ->
>             ?DUMP({send_and_receive, received, Response}),
>             Response
>     end.
>
> loop() ->
>     receive
>         {From, hello} ->
>             ?DUMP(hello),
>             From ! {self(), hi},
>             loop();
>         {_From, exit} ->
>             ?DUMP(exit),
>             void
>     end.
>
> _______________________________________________
> 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/20081209/25534247/attachment.htm>


More information about the erlang-questions mailing list