[erlang-questions] Process Message Queues
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 <> wrote:
>> ()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).
>> ()2> Pid ! hello.
>> ()3> Pid ! goodbye.
>> ()4> Pid ! message_d.
>> ()5> Pid ! message_e.
>> ()6> erlang:process_info(Pid,message_queue_len).
> 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 :)
More information about the erlang-questions