Nezteb

Nezteb

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?

Showing Posts 1 to 6

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.

dimitarvp

dimitarvp

My apologies for a direct ping but… @rvirding? Is this an interesting topic for you to comment on?

Nezteb

Nezteb OP

Yeah I realized my query should have been something like (/:proc_lib\b/ OR /:sys\b/). So, a slightly modified query gives me some good ideas. :smile:

I understand a bit more after reading the “User-Defined Behaviours” section of the docs, though I’m still curious when others prefer to use :proc_lib for their processes!

RudManusachi

RudManusachi

Also there are many results in GitHub with proc_lib: (colon in the end) :wink:

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 #5
fmcgeough

fmcgeough

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

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
subsaharancoder
I’ve followed the Phoenix LiveView file upload code here Uploads — Phoenix LiveView v1.0.0-rc.7 and so far everything works just fine wit...
New
mohsen
I’m using an Umbrella project for a Phoenix application, and I want to have one Ecto Repo and one PostgreSQL database shared by all apps....
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Damirados
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews