Astarno

Astarno

Inspect the environment of a Process

What would be the correct approach to inspect the current environment of a Process? For example, if I would have a simple Process defined as below:

defmodule Stack do
  def loop(state, ctr) do
    receive do
      {_from, :push, value} ->
        loop([value | state], ctr + 1)

      {from, :pop} ->
        [h | t] = state
        send(from, {:reply, h})
        loop(t, ctr)
    end

    loop(state)
  end
end

Here the environment consists of two variables (parameters of the loop function) at any point during execution. How could I easily get a list of such variables and their associated value as such:

state: value
ctr: value

I’m new to Elixir. Thanks in advance!

Most Liked

lucaong

lucaong

When implementing something similar with a GenServer one can use :sys.get_state(pid) for debugging, but in this case I don’t see a way without modifying the code. In your example the state is the arguments of the recursive call to loop, so one needs to tap into the function to inspect the state.

What’s your use case?

lucaong

lucaong

Hi @Astarno,
In your example, you could add a receive clause to return the state:

defmodule Stack do
  def loop(state, ctr) do
    receive do
      {_from, :push, value} ->
        loop([value | state], ctr + 1)

      {from, :pop} ->
        [h | t] = state
        send(from, {:reply, h})
        loop(t, ctr)

      {from, :inspect} ->
        send(from, {state, ctr})
        loop(state, ctr)
    end
  end
end

Now you can send a message like {self(), :inspect} and receive the state as a message:

iex(1)> pid = spawn(Stack, :loop, [[], 0])
#PID<0.127.0>
iex(2)> send(pid, {self, :inspect})
{#PID<0.105.0>, :inspect}
iex(3)> flush
{[], 0}
:ok
iex(4)> send(pid, {self, :push, "foo"})
{#PID<0.105.0>, :push, "foo"}
iex(5)> flush
:ok
iex(6)> send(pid, {self, :inspect})
{#PID<0.105.0>, :inspect}
iex(7)> flush
{["foo"], 1}
:ok
RudManusachi

RudManusachi

In general the better practice would be to use built in OTP abstractions like GenServer, as @lucaong mentioned, rather than plain loop with receive do, and we can use :sys.get_state, :sys.replace_state, :observer and other tools to inspect running system in realtime without changing the code of the project.

Where Next?

Popular in Questions Top

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New

Other popular topics Top

pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement