See all messages in process's mailbox with pid XY

2,789 views
Skip to first unread message

tomaz....@gmail.com

unread,
May 17, 2015, 2:14:22 PM5/17/15
to elixir-l...@googlegroups.com
Is it possible to see all messages that are in mailbox of a process with a given pid? I know it is possible to use flush in console process. But flush removes all messages and I just want to see them and not just for console mailbox, but for any mailbox I know.

Saša Jurić

unread,
May 17, 2015, 2:24:08 PM5/17/15
to elixir-l...@googlegroups.com
You can use :erlang.process_info

E.g.:

iex(1)> shell_process = self

iex(2)> spawn(fn ->
...(2)>   send(shell_process, :foobar)
...(2)>   :erlang.process_info(shell_process, :messages) |> IO.inspect
...(2)> end)

{:messages, [:foobar]}

You can have a GUI interface via observer application. Just start it from the shell with :observer.start, head to Processes tab, find the target process, double click it, and in messages you should see a snapshot of the message queue.

It is also possible to use Erlang's tracing via :sys, :dbg module, and :erlang.trace/3 function to dynamically start traces for various events. Among other things, you can trace messages sent to or from particular process(es).

Tomaž Žlender

unread,
May 17, 2015, 3:05:56 PM5/17/15
to elixir-l...@googlegroups.com
Awesome! Thank you :)

--
You received this message because you are subscribed to a topic in the Google Groups "elixir-lang-talk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-lang-talk/sF84esrPSoc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/bcfe033f-e532-4c33-ac5e-87f72d977c0b%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Robert Virding

unread,
May 17, 2015, 5:51:51 PM5/17/15
to elixir-l...@googlegroups.com
You should be aware that getting all the messages in another process's mailbox can be a very costly operation. All the messages are copied from the other process to your process. Processes don't share data. To be safe it might be better to first check the length of the message queue using Process.info(pid, :message_queue_len).

Robert
Reply all
Reply to author
Forward
0 new messages