Using port drivers in OTP

Is there some pattern for using port drivers in OTP?

My plan is running some external programs for some amount of time.

Explore the Erlang documentation’s tutorial on C Ports, an essential feature that facilitates seamless integration between Erlang and C programming languages. The tutorial provides detailed insights and guidance, offering developers a comprehensive resource to understand the intricacies of C Port. You also can find useful Building C/C++ | Rebar3 documentation useful or at Erlang.mk User Guide - related to which tool for build project you want to use. Plus, you can take a look to this topic Erlang Nodes and C Nodes.

2 Likes

I might haved asked the wrong question. My plan is calling out for ffmpeg via open_port.

There is no specific pattern for it. There is erlexec library that implements some convenience, but I have no personal experience with it. If you’re not planning to leverage it, several notes that I accumulated while working on similar externally running code. Most of these notes are clearly visible in OTP peer module (otp/lib/stdlib/src/peer.erl at master · erlang/otp · GitHub).

  • for every instance of the external executable, have a supervising Erlang process (peer implements a gen_server for that purpose)
  • you may need to use OS level signals (e.g. kill on Unix, executed with the same open_port approach) to control external executable behaviour)
  • be aware of how the executable operates with stdin/stdout (e.g. it may crash if stdin is abruptly closed, or stdout/stderr is not functioning)
1 Like

Thanks!