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

itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
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
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
gazoon
I want to know absolute current module path. In python i could do that: os.path.abspath(__file__) Does elixir have anything similar?
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
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
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
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
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
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

We're in Beta

About us Mission Statement