Erlang Process doesn't receive messages

Shawn Pearce spearce@REDACTED
Mon Jun 9 20:44:16 CEST 2003


In the shell, you have bound Msg to the value of 1.

So during the first receive:

99> receive Msg -> Msg after 3000 -> timeout end.

Msg is bound to 1.

Now later on, in all other receive calls, you really have:

104> process_info(self(), messages).
{messages,[1,2]}
105> receive 1 -> 1 after 3000 -> timeout end.
1
106> receive 1 -> 1 after 3000 -> timeout end.
timeout

as there is no message '1' in the mailbox.

You must forget the binding of Msg using f(Msg) (which only works
in the shell), or use another variable:

105> receive Msg1 -> Msg1 after 3000 -> timeout end.
1
106> receive Msg2 -> Msg2 after 3000 -> timeout end.
2



Serge Aleynikov <serge@REDACTED> wrote:
> During the following test, I discovered a non-intuitive behavior that 
> I'd appreciate to receive some help with.
> 
> Given two processes: client (shown as a Erlang shell process), and 
> server.  The server implements the following loop in a process separate 
> from client:
> 
> loop() ->
>     receive
>         stop ->
>             stopped;
>         {test, From, Arg} ->
>             timer:sleep(3000),
>             From ! Arg,
>             loop()
>     end.
> 
> Perform the commands below from the client process shell.  What is not 
> clear in this code is that despite the fact that the client has messages 
> in its mailbox, the receive command times out without removing messages 
> from the mailbox.
> 
> Can anyone explain this?
> 
> Thanks,
> 
> Serge
> 
> 94> Pid = server:start().
> <0.139.0>
> 95> process_info(self(), messages).
> {messages,[]}
> 96> Pid ! {test, self(), 1}.
> {test,<0.142.0>,1}
> 97> process_info(self(), messages).
> {messages,[]}
> 98> process_info(self(), messages).
> {messages,[1]}
> 99> receive Msg -> Msg after 3000 -> timeout end.
> 1
> 100> Pid ! {test, self(), 1}.
> {test,<0.142.0>,1}
> 101> Pid ! {test, self(), 2}.
> {test,<0.142.0>,2}
> 102> process_info(self(), messages).
> {messages,[1]}
> 103> process_info(self(), messages).
> {messages,[1]}
> 104> process_info(self(), messages).
> {messages,[1,2]}
> 105> receive Msg -> Msg after 3000 -> timeout end.
> 1
> 106> receive Msg -> Msg after 3000 -> timeout end.
> timeout
> 107> receive Msg -> Msg after 3000 -> timeout end.
> timeout
> 108> process_info(self(), messages).
> {messages,[2]}
> 109> os:type().
> {win32,nt}
> ------------------------
> 

-- 
Shawn.

  Be a better psychiatrist and the world will beat a psychopath to your door.



More information about the erlang-questions mailing list