Erlang - check mailbox messages one at a time

I am trying to check a node messages from other nodes, but in some other way besides flush() , because the message size is quite large and this does not help. In addition, I can see messages with erlang:process_info(self(), messages_queue_len). but I would like to somehow extract one message at a time in some variable for debugging purposes.

+4
source share
3 answers

receive - erlang primitive for receiving messages from the mailbox.

See: http://www.erlang.org/doc/getting_started/conc_prog.html#id2263965

If you just want to receive the first message in the debugging shell, you can try to define such a pleasure:

 1> self() ! foo. foo 2> F = fun() -> receive X -> X end end. #Fun<erl_eval.20.67289768> 3> F(). foo 
+1
source

You might want to take a look at the dbg module in Erlang.

Launch the indicator:

 dbg:tracer(). 

Track all received messages (r) by a process (in this case self ()):

 dbg:p(self(), r). 

More details here .

+7
source

or you can use:

 1> F = fun() -> receive X -> {message, X} after 0 -> no_message end end. #Fun<erl_eval.20.111823515> 2> F(). no_message 3> self() ! foo. foo 4> self() ! bar. bar 5> F(). {message, foo} 6> F(). {message, bar} 

... to prevent blocking

+1
source

Source: https://habr.com/ru/post/1313273/


All Articles