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

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
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
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
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
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