[erlang-questions] Process Message Queues

Geoff Cant nem@REDACTED
Fri Apr 23 22:40:56 CEST 2010


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.

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 :)

--Geoff


More information about the erlang-questions mailing list