Today I Learned

A Hashrocket project

Ready to join Hashrocket? Find Openings here and apply today.

Inspecting The Process Message Queue

A core tenant of Elixir is message passing between processes. So, if a process is sent a message, where does that message go? What happens if it gets sent many messages? The function allows us to inspect the message queue.

First, let’s send some messages (to ourself) and then keep an eye on the length of the message queue as we go.

> send self(), {:error, "this is bad"}
{:error, "this is bad"}
>, :message_queue_len)
{:message_queue_len, 1}
> send self(), {:hello, "world"}
{:hello, "world"}
>, :message_queue_len)
{:message_queue_len, 2}

Now, I am curious what those specific messages are. Let’s ask for the messages that are in the message queue.

>, :messages)
{:messages, [error: "this is bad", hello: "world"]}

There are a lot of other things that can tell us about a process. See the Erlang docs for process_info for more details.

Looking for help? At Hashrocket, we 💜 Elixir! From our many Elixir client projects, to sponsoring the Chicago Elixir Meetup, to the source code for this application, we are invested in this community. Contact us today to talk about your Elixir project.