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)
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted”
Version...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #blog-post
- #ai
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
hauleth
Just don’t use
Agentif you want to handle your own messages.denna
Actually i want to use send_after from another process.
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_statemwhich is an abstracted behaviour for that exact thing instead of shoehorning that into agen_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_serveror writing your own simple process to handle it (usually you’ll want thegen_serveras its interface handles a lot of things for you)denna
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
Agentis thin wrapper overGenServer, so the performance should be similar.And your example using
GenServerwill not be much more complex:denna
But doesn’t sending functions as messages makes sending more expensive?
hauleth
You send functions as a messages in
Agentcase. So if it is indeed true (I highly doubt that if these messages are on local machine) thenGenServerwill be more performant one, as it send just simple atoms.kokolegorille
Many don’t use Agent (and prefer GenServer), and many don’t use :timer library (and prefer Process.send_after, or similar)
This is something You might learn from experience.
derek-zhou
Does
Process.send_after/3work if the calling process quit before the timeout? If I want to do something similar, I’d cast adelay_updatemessage to my gen_server, and within my gen_server I willProcess.send_after/3to self() to postpone the update. It involves 2 messages but it feels safer.hauleth
What you mean by “works”? It will not fail, but the message will be discarded, as there is no receiver.