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

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
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
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement