[erlang-questions] Process Message Queues

Robert Virding rvirding@REDACTED
Fri Apr 23 22:56:15 CEST 2010


On 23 April 2010 22:40, Geoff Cant <nem@REDACTED> wrote:
> On 2010-04-23, at 12:52 , Per Melin wrote:
>
>> On Fri, Apr 23, 2010 at 6:49 PM, Evans, Matthew <mevans@REDACTED> wrote:
>>> (scm@REDACTED)1> Pid = spawn(fun() -> receive message_a -> error_logger:info_msg("Got message_a~n"); message_b -> error_logger:info_msg("got message_b~n"); message_c -> receive message_d -> error_logger:info_msg("Got message_d~n") end end end).
>>> <0.19842.0>
>>> (scm@REDACTED)2> Pid ! hello.
>>> hello
>>> (scm@REDACTED)3> Pid ! goodbye.
>>> goodbye
>>> (scm@REDACTED)4> Pid ! message_d.
>>> message_d
>>> (scm@REDACTED)5> Pid ! message_e.
>>> message_e
>>> (scm@REDACTED)6> erlang:process_info(Pid,message_queue_len).
>>> {message_queue_len,0}
>>
>> Put the first line with the spawn in a function in a module. Compile
>> it and start your experiment by calling the function. The rest should
>> then behave as expected.
>
> Am I right in saying that this interesting result is due to the fact that shell code (evaluated by erl_eval) is different to module code (evaluated by beam)? If I remember correctly, to implement receive erl_eval drains the message queue into a list and then simulates what beam would do when evaluating a receive statement. Hence the oddness with message_queue_len, and why it behaves as expected when written as module code.

I think I remember reading somewhere that process_info doesn't always
return the true current status of the process, apparently some of the
data isn't updated continually. Or something like that. However, when
I checked the docs I couldn't find anything about it, so it has either
gone away or was never there.

> The differences between shell code behaviour and module code behaviour often seem to be a source of confusion. Even though I know they're different, I was bitten myself recently by unicode handling differences between the two and spent a few frustrating hours figuring it out :)

Didn't know about unicode differences, only that you can get slightly
different handling of receive because of the way the message buffer is
handled. I would think it very strange if you get different behaviour
from somethin that was called.

Robert


More information about the erlang-questions mailing list