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:

http://erlang.org/doc/efficiency_guide/commoncaveats.html

This is something You might learn from experience.

hauleth

hauleth

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

amnu3387

amnu3387

I think that’s what @hauleth is saying, if you need to receive messages on the Agent, then it’s not the right thing to use.

There’s several abstractions already written in both erlang and elixir and they all have a sort of “utility field”.

Agents are for keeping/retrieving state. GenServers are generic server interfaces, so they’re modelled as a behaviour that you can customise to handle your own message needs.

They can also do a slight impersonation of a state machine, but if you need actual state machine semantics, timeouts, stepped flows, etc then probably it’s easier to write a proper gen_statem which is an abstracted behaviour for that exact thing instead of shoehorning that into a gen_server, Tasks are for doing something and exiting, and so on.

If you need your Agent to receive messages and handle them then you’re either looking at gen_server or writing your own simple process to handle it (usually you’ll want the gen_server as its interface handles a lot of things for you)

hauleth

hauleth

You send functions as a messages in Agent case. So if it is indeed true (I highly doubt that if these messages are on local machine) then GenServer will be more performant one, as it send just simple atoms.

Where Next?

Popular in Questions Top

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
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
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
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

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
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
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
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
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
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

We're in Beta

About us Mission Statement