Nezteb

Nezteb

:proc_lib vs GenServer?

Earlier today, Code Sync posted the recording of @mhanberg’s Code BEAM America 2022 talk: “OTP Process Abstractions with :proc_lib

I enjoyed the talk! After watching it, I went over to the OTP docs to read more about :proc_lib.

This module is used to start processes adhering to the OTP Design Principles. Specifically, the functions in this module are used by the OTP standard behaviors (for example, gen_server and gen_statem) when starting new processes. The functions can also be used to start special processes, user-defined processes that comply to the OTP design principles. For an example, see section sys and proc_lib in OTP Design Principles.

I wondered if any large Elixir projects make direct use of :proc_lib, so I went looking:

So I was left with one question: Mitchell said there are two common ways of creating a “process abstraction”: wrapping a GenServer and using :proc_lib. When would one reach for :proc_lib over a vanilla GenServer (or even just a plain process)? What are the pros/cons of each?

Most Liked

rvirding

rvirding

Creator of Erlang

My way of viewing this is that :proc_lib is a tool which is used for building OTP compliant servers. It is used for building the standard OTP behaviours like :gen_server/GenServer but you can of course use it for building your own OTP compliant processes, called in the OTP docs for special processes.

Why would you want to build your own processes and not just use the standard provided ones? Well maybe you need something which is completely OTP compliant so it fits in and can be run by supervisors but just doesn’t really fit to be built using the the standard behaviours, e.g. maybe you want much better control over message handling than you can get with a GenServer.

So building your own special processes is actually not that difficult. There are 5 rules to follow and the first one is to start it using :proc_lib. You then need to detect and handle system messages in the right way but most of that work is done for you with the :sys module which you call in the right places. Finally you need to terminate the process at the right time in the right way. Quite simple really.

All this is an hour or so in our (Erlang Solutions) Erlang OTP course. :wink: :smile:

BTW the :sys module contains a lot of useful functions for managing OTP behaviours.

15
Post #6
mhanberg

mhanberg

Creator of elixir-tools

For now, will drop this GitHub Search query: Sign in to GitHub · GitHub.

I think the gist of the answer is that you would use it when you need very fine tuned behavior that differs from a GenServer, or when you want custom debugging/tracing capabilities.

José or some Erlang folks can probably be more specific.

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
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
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 record...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
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
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
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