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?

Marked As Solved

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

Also Liked

mhanberg

mhanberg

Expert LSP Core Team

For now, will drop this GitHub Search query: Code search results · 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.

Last Post!

fmcgeough

fmcgeough

The kafka_protocol library (used by brod) uses proc_lib. kafka_protocol/src/kpro_connection.erl at master · kafka4beam/kafka_protocol · GitHub.

Where Next?

Popular in Questions Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
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
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130579 1222
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
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
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement