bobbypriambodo

bobbypriambodo

Agent, Task, GenServer, GenEvent: what do you use it for?

I’m trying to understand more about Elixir OTP concepts, particularly the ones mentioned in the title. The tutorials gave examples with KV buckets, but I think I need more realistic examples.

Can anyone give examples on how you use them in your apps/libraries? (I’m particularly interested in their usage on a Phoenix app, but if you do use it on Elixir app it’s okay too.)

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

First, a nit pick. There are only Elixir apps. Some Elixir apps use phoenix, some do not, but there are only Elixir apps.

Let’s start with the simplest case, tasks. We have an async job worker system, and every job is run inside a task. This makes it easy for the worker process to monitor what happens to the job. It also makes for less work for the garbage collector because any memory the job uses can be all easily reclaimed when the process quits.

GenServers: These get used for a variety of things. I have a slack bot process that aggregates some info and periodically shoves it in slack. I have processes that represent sensors out in warehouses monitoring temperature, and these genservers give us notifications if they haven’t heard from a sensor in X amount of time (amongst other things).

Agent: Basically just a simplified genserver. Never used one.

GenEvent: Used it a long time ago, but there were pain points. Technically I still use it cause I have a customer Logger backend and IIRC it uses GenEvent.

Not mentioned: :gen_statem I used recently in some sensor management software. Each connected sensor gets its own state machine process that ensures it walks through the right states / transitions as it gets configured / upgraded / etc.

bobbypriambodo

bobbypriambodo

True! I wonder why I wrote that, especially when I just reread this “Phoenix is not your application” thread. Sorry about that :sweat_smile: I meant when you are mainly using Phoenix for your app.

Where Next?

Popular in Questions Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
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
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
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
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
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
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

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
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
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
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement