denna

denna

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)

Showing Posts 1 to 10

hauleth

hauleth

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

denna

denna OP

Actually i want to use send_after from another process.

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)

denna

denna OP

I still think that Agent is quite efficient (in terms of lines to be written compared to GenServer) and i think it makes sense to update its state in the future. I solved my need for now using :timer.apply_after/4.
In terms of processing power its not efficient.
The Erlang documentation highlights that Process.send_after is more efficient than :timer.apply_after/4 . I guess that GenServer is more efficient than Agent. Am I right about this?

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
denna

denna OP

But doesn’t sending functions as messages makes sending more expensive?

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.

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.

derek-zhou

derek-zhou

Does Process.send_after/3 work if the calling process quit before the timeout? If I want to do something similar, I’d cast a delay_update message to my gen_server, and within my gen_server I will Process.send_after/3 to self() to postpone the update. It involves 2 messages but it feels safer.

hauleth

hauleth

What you mean by “works”? It will not fail, but the message will be discarded, as there is no receiver.

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New

Other Trending Topics Top

georgeguimaraes
Hi all, I want to show you Transmuter, a library that runs Hugging Face ONNX models on any Nx backend. GitHub: GitHub - georgeguimarae...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews