kuon
Making async code synchronous
I wrote a NIF which have function that can take a long time to run. To prevent the scheduler from being overloaded with long running functions (even if marked at dirty), I made my NIF threaded and it sends message back to erlang using enif_send.
I want to make those call synchronous from a “programmer point of view”. Similar to a Process.sleep() call.
I was wondering what was the best approach. To avoid messing with the caller message inbox and to have an isolated process to receive the message, I thought of using something like this:
task = Task.async(fn ->
call_nif(self())
receive do
<wait for nif response>
end
end)
Task.await(task)
Would that be a good approach?
Most Liked Responses
krasenyp
It could work but in my opinion it’s best to use a yielding NIF or spawn an extra process and use local means of communication.
LostKobrakai
I’ll remove the matches message from the inbox – but it’ll leave anything else as is.








