在“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}.
但结果是:
1> mytest:start().
true
2> mytest:test().
Low priority: msg2
{high_p,msg3}
High priority: msg1
High priority: msg3
这似乎不对,所以我将代码更改为:
do_receive() ->
receive
{high_p, Message} ->
io:format("High priority: ~p~n", [Message]),
do_receive()
after 0 ->
receive
{high_p, _} = Other ->
self() ! Other,
do_receive();
{low_p, Message} ->
io:format("Low priority: ~p~n", [Message]),
do_receive()
end
end.
结果如下(所有高优先级消息都在低优先级消息之前打印)。
1> mytest:start().
true
2> mytest:test().
High priority: msg3
{high_p,msg3}
High priority: msg1
Low priority: msg2
这是实现我的目标的正确方法吗?并且可以在 gen_server handle_info/2 中实现不同的优先级吗?