kdlogan19

kdlogan19

Error: Process is not alive while implementing a Stack through genserver

Hello everyone,

I am implementing a stack with Genserver, but I keep getting an error saying there is no process alive. Please go through the code below.

defmodule Stack.Server do
  use GenServer

  def start(_elements) do
      IO.puts "Hello"
      GenServer.start_link(__MODULE__, [4,5,"cool"])
    end

  def init(init_num) do
    {:ok, init_num}
  end


  def handle_call( _from,:pop,[h|t]) do
    IO.inspect [h|t]
    IO.inspect [t]
    IO.puts "handling call"
    {:reply, {:ok,h}, t}
  end

  def handle_cast({:push,value},state) do
    IO.inspect value
    IO.puts "handling cast"
    {:reply, {:ok,value}, [value|state]}
  end

  def pop(pid) do
      IO.puts "Inside popping stack"
      IO.inspect pid
       GenServer.call(:pops, pid)
   end

   def push(stack, item) do
     IO.puts "Inside pushing stack"
     GenServer.cast(stack,{:push,item})
   end

end

{:ok, pid} = Stack.Server.start [1,2,3]
Stack.Server.pop(pid)
Stack.Server.push(pid, 10)
Stack.Server.pop(pid)

And the error message says:

Compiling 2 files (.ex)
Hello
Inside popping stack
#PID<0.121.0>

== Compilation error in file lib/stack/server.ex ==
** (exit) exited in: GenServer.call(:pops, #PID<0.121.0>, 5000)
    ** (EXIT) no process: the process is not alive or there's no process currently associated with the given name, possibly because its application isn't started
    (elixir) lib/gen_server.ex:999: GenServer.call/3

Thanks a lot in advance
PS: I am a beginner.

Most Liked

peerreynders

peerreynders

Welcome to the forum!

GenServer.call(:pops, pid)

Have a look at GenServer.call/3

While you are there have a look at the GenServer stack example.

ityonemo

ityonemo

Double check

  1. your function parameter order on genserver.call
  2. your function matching order when you handle :pop
  3. your atom literals

Later, you’re gonna want to double check the spec on handle_cast.

peerreynders

peerreynders

Check the order of the arguments in your handle_call/3 callback function.

Last Post!

ityonemo

ityonemo

I know some languages are more permissive, but elixir is sane, you can’t do def subtract(a, b) with an unexpected matching order.

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
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
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130579 1222
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44608 311
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
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