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
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
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
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.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









