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

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
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
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44265 214
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

We're in Beta

About us Mission Statement