From 6a34dda04be70df18c50ea160b87534852122e7a Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Thu, 13 Apr 2017 08:27:19 -0500 Subject: [PATCH] Add Inspecting The Process Message Queue as an elixir til --- README.md | 3 +- .../inspecting-the-process-message-queue.md | 33 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 elixir/inspecting-the-process-message-queue.md diff --git a/README.md b/README.md index 337a3c2..df3236e 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/). For a steady stream of TILs from a variety of rocketeers, checkout [til.hashrocket.com](https://til.hashrocket.com/). -_524 TILs and counting..._ +_525 TILs and counting..._ --- @@ -111,6 +111,7 @@ _524 TILs and counting..._ - [Execute Raw SQL In An Ecto Migration](elixir/execute-raw-sql-in-an-ecto-migration.md) - [Expose Internal Representation](elixir/expose-internal-representation.md) - [Include Captures With String.split](elixir/include-captures-with-string-split.md) +- [Inspecting The Process Message Queue](elixir/inspecting-the-process-message-queue.md) - [List Functions For A Module](elixir/list-functions-for-a-module.md) - [Listing Files In IEx](elixir/listing-files-in-iex.md) - [Pattern Matching In Anonymous Functions](elixir/pattern-matching-in-anonymous-functions.md) diff --git a/elixir/inspecting-the-process-message-queue.md b/elixir/inspecting-the-process-message-queue.md new file mode 100644 index 0000000..4b1d317 --- /dev/null +++ b/elixir/inspecting-the-process-message-queue.md @@ -0,0 +1,33 @@ +# 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 `Process.info/2` 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. + +```elixir +> send self(), {:error, "this is bad"} +{:error, "this is bad"} +> Process.info(self(), :message_queue_len) +{:message_queue_len, 1} +> send self(), {:hello, "world"} +{:hello, "world"} +> Process.info(self(), :message_queue_len) +{:message_queue_len, 2} +``` + +Now, I am curious what those specific messages are. Let's ask +`Process.info/2` for the messages that are in the message queue. + +``` +> Process.info(self(), :messages) +{:messages, [error: "this is bad", hello: "world"]} +``` + +There are a lot of other things that `Process.info/2` can tell us about a +process. See [the Erlang docs for +`process_info`](http://erlang.org/doc/man/erlang.html#process_info-2) for +more details.