Gatica

Gatica

Calling Elixir process from a Port Driver

I’m trying to figure out a way to initiate a call to the Elixir application from a linked port driver. I have a library in C, which functions as a server. It creates a thread when it’s initialised from Elixir, which sevices network requests in C. I can make calls from Elixir down to the driver and send messages to it. However, if the server needs to respond to a request, it cannot make calls back up to Elixir. I thought this would be straightfoward with driver_output() function, but then I don’t have the driver handle and I suppose the message in Elixir would be handled asynchronously too.

Any suggestions as to how this can be done?

Most Liked

garazdawi

garazdawi

Erlang Core Team

You cannot call Erlang/Elixir from a driver or nif.

The thread that does the request could wait on a futex (or similar) after sending a message to the process that needs to run some Erlang/Elixir code. Then when the process is done it can call the driver/nif again and deliver the response and trigger the futex.

It is not easy to do things this way, but it is possible. For instance, I think that the wx application in Erlang handles some of the wx callbacks this way.

evadne

evadne

I have a cracking idea.

• You might be able to cheat by passing a reference to a separately handled UNIX socket.

• Alternatively, make your external thing a C node and integrate Erlang Distribution (i.e. change the design). Still, have your Elixir app spawn & manage it, possibly via saleyn/erlexec or some other libraries that were proven to be robust.

Making the external thing into its own C / Java node, is a viable option, and I have used this approach with great success.

Last Post!

Qqwy

Qqwy

TypeCheck Core Team

I recently asked about a very similar situation (calling a function from a NIF) over on the Erlang forums. There is no straightforward way to do this, but there are a number of interesting workarounds.
The core approach is similar to what @garazdawi also mentions: Use a separate Elixir/Erlang process which you exchange messages with. To hide this async communication inside a function that expects to run in a normal synchronious fashion, using any kind of mutex to wait for a reply (/or crash) from the process.

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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
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
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
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
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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
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
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New