denna

denna

Send_later to Agent

I like to make use of Process.send_later to send a message to an Agent.
When I understand this tutorial right than one needs to implement handle_info to handle the message send by send_after. Agent doesn’t implement handle_info. Only gen_server does so.

As an example one could use the counter from the documentation:

defmodule Counter do
  use Agent

  def start_link(initial_value) do
    Agent.start_link(fn -> initial_value end, name: __MODULE__)
  end

  def value do
    Agent.get(__MODULE__, & &1)
  end

  def increment do
    Agent.update(__MODULE__, &(&1 + 1))
  end
end

Counter.start_link(1)

Process.send_after(Counter,:increment,1000)

Most Liked

hauleth

hauleth

Agent is thin wrapper over GenServer, so the performance should be similar.

And your example using GenServer will not be much more complex:

defmodule Counter do
  use GenServer

  def start_link(initial_value) do
    GenServer.start_link(__MODULE__, initial_value, name: __MODULE__)
  end

  def value, do: GenServer.call(__MODULE__, :get)
  def increment, do: GenServer.call(__MODULE__, :inc)

  @impl true
  def init(value), do: {:ok, value}

  @impl true
  def handle_call(:inc, state), do: {:reply, :ok, state + 1}
  def handle_call(:get, state), do: {:reply, state, state}
end
kokolegorille

kokolegorille

Many don’t use Agent (and prefer GenServer), and many don’t use :timer library (and prefer Process.send_after, or similar) :slight_smile:

This is something You might learn from experience.

hauleth

hauleth

Just don’t use Agent if you want to handle your own messages.

Last Post!

derek-zhou

derek-zhou

Process.send_after/4 looks up the pid from registered name at the time of timer expiration. So Process.send_after(Crap, :hi, 1000) will always succeed. And the message will be silently dropped because there is no such process. In comparison, if you send(MyGenServer, {:delayed_hi, 1000}) and in the GenServer you handle the custom message with a Process.send_after(self(), ...) it will be safer because:

  • If you mis-typed the name in the first send, the sending process will crash
  • The second send to self() has no lookup and will not drop message so long as it is still alive.

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
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 records...
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
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
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
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
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

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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

We're in Beta

About us Mission Statement