在"erlang编程"一书中,在这个堆栈溢出问题中,我看到我们可以使用以下命令为邮箱中的erlang消息提供不同的优先级:
我尝试用这段代码实现类似的东西:
-module(mytest).
-export([start/0, test/0]).
start() ->
register(?MODULE, spawn(fun() -> do_receive() end)).
do_receive() ->
receive
{high_p, Message} ->
io:format("High priority: ~p~n", [Message]),
do_receive()
after 0 ->
receive
{low_p, Message} ->
io:format("Low priority: ~p~n", [Message]),
do_receive()
end
end.
test() ->
mytest ! {high_p, msg1},
mytest ! {low_p, msg2},
mytest ! {high_p, msg3}.
Run Code Online (Sandbox Code Playgroud)
但结果是:
1> mytest:start().
true
2> mytest:test().
Low priority: msg2
{high_p,msg3}
High priority: msg1
High priority: msg3
Run Code Online (Sandbox Code Playgroud)
这似乎不对,所以我将代码更改为:
do_receive() ->
receive
{high_p, Message} ->
io:format("High priority: ~p~n", [Message]), …Run Code Online (Sandbox Code Playgroud) erlang ×1