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.

Where Next?

Popular in Questions Top

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
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
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
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
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
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New

Other popular topics Top

dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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
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
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I'm a nov...
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
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement