akash-akya
Exile is an alternative to beam ports for running external programs. It provides back-pressure using non-blocking io, and tries to fix all issues associated with ports.
Rationale and issues associated with exiting approaches are mentioned here
This is another stab at solving the issues associated with running an external command. My other approach ExCmd uses named pipes for solving these issues. But, since :file functions are blocking operations in the beam, this can cause issues. As of now, there are no non-blocking file operations available in :file module. So the only way to do non-blocking io is using NIF/port-driver.
Exile.stream! is a stream interface for interacting with the external program. It works with both Enumerable and Collectable. Apart from being compostable, stream handles closing stdin and terminating the external process.
Exile.stream!(~w(ffmpeg -i pipe:0 -f mp3 pipe:1), input: File.stream!("music_video.mkv", [], 65536))
|> Stream.into(File.stream!("music.mp3"))
|> Stream.run()
Key differences when compared to other middleware based libraries and port
- fixes issues associated with ports
- no more zombie process
- can selectively close stdin
- its is demand-driven (back-pressure)
- it uses
enif_selectfor asynchronous io, so it utilizes resources efficiently - uses non-blocking read/write system calls. so it can never block the scheduler
- it does not use any middleware
- no additional os process. no performance/resource cost
- no need to install any external command
- can run many external programs in parallel without adversely affecting schedulers (when compared to ExCmd)
- stream abstraction for interacting with the external program
- should be portable across POSIX compliant operating systems (not tested)
Non-blocking io can be used for other interesting things. Such as reading named pipe (FIFO) files. Exile.stream!(~w(cat data.pipe)) does not block schedulers so you can open hundreds of FIFO files unlike default :file module.
Please check the project page for more detail.
Note: Exile is experimental and it is still work-in-progress
feedback is welcome ![]()
Trending in Announcing
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #elixirconf-us
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #elixirconf-eu
- #processes
- #security
- #api
- #forms
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
dimitarvp
Very, very nice. Love it.
Just one note, might be misinformed: I am worried that with a C dependency the crate might not compile at all on Windows – have you tried it? Or are you targeting UNIX-es only?
That’s the reason I am only doing NIFs with Rust – got impressed in the past how quickly and easily it compiles stuff on Windows without complaints.
akash-akya
Thank you
It uses the POSIX API. so currently, it is for UNIX like systems only.
We can have different implementation for windows, but I don’t have a windows machine to test it properly.
I like to use Rust, but I think this case different. Most of the code associated with system calls, so using C is straight-forward and it can be used with any POSIX complaint os without relying on rust compiler presence.
dimitarvp
I see, thank you.
jayjun
Great stuff, need more explorations in this area! Two questions,
Forking in the VM briefly causes a memory explosion, that’s why
erl_child_setupexists to spawn ports. Have you done a comparison?How do you prevent misbehaving programs from becoming orphans? Misbehaving as in those that don’t exit after standard input is closed.
akash-akya
Thank you
I thought of this approach before. But in most modern os
fork()is copy-on-write. so memory explosion is not an issue (unless I’m missing something). But keeping separate processes forexechas other advantages which are mentioned here. This can be done, but I think the current approach is much simpler. And this process will be like a “shared” resource among all schedulers and I prefer to avoid that. also, we might have to monitor this os process and deal with all the concern that comes with it. If we really need it, we can add it in the future I think.The executed external program is tied to a beam process, and this beam process will be monitored by another a “watcher” beam process. Watcher makes sure to terminate the spawned program when the beam process dies for whatever reason. It does in the order: polite close stdin → SIGTERM → SIGKILL.
I would like to know if there are better approaches than this
jayjun
The forker spawn driver was added fairly recently in ERTS 8.0 (search for
erl_child_setup) because of 3-5x better performance, even 10x. This is way after Linux implemented copy-on-write.So I suspect the memory explosion comment still refers to modern kernels. I found a good read.
fork() is evil; vfork() is goodness; afork() would be better; clone() is stupid
Regardless, now that you’ve brought
fork()back into the VM, I feel it’s a great opportunity to do a proper comparison. Judging from the above, the difference isn’t trivial.The scenario I imagined is when the Erlang node crashed or is forcefully killed. An external shim can detect when that happens then clean up by killing all child processes before exit.
mbklein
This looks very cool, and I’m trying to convert some code that’s currently using ports to use Exile instead, but I’ve having some trouble. The code is in JavaScript, and I’m having trouble trying to get it to handle the input stream. I’ve managed to reduce it to a super-simple test case that just copies
stdintostdout:Here’s the JS:
And here’s the Elixir:
If I run it from the command line…
But if I try to call it from iex…
…and it sits there blocking forever until I
^Cmy way out.I’ve tried a bunch of different ways to read input on the JS side:
process.stdin.read(),process.stdin.on('data', ...).on('end', ...)handlers,fs.readFileSync(0), and whatever else I could think of. Exile never seems to send it any data.Any thoughts? Would you prefer I open an issue in the GitHub repo instead?
akash-akya
Ok, I was thinking about the memory overhead. But its CPU overhead due to memory pages update.
I did basic benchmark on mac. forking from beam process is indeed around 3 times slower than
erl_child_setup(sometimes 4.5 times). Note that it is still ~1000 forks per second. practically speaking, I’m not sure if raw forking speed is important, because we usually hit other system limits before we start to operate at this rate. Nevertheless, this is nice to have, thanks for pointing it out, I’ll look into improving this. Here are the resultsYes, exile does not kill processes if beam crashes or if the beam is killed with SIGKILL. This is similar to how all shells and other languages I checked behave. Because any kind of fix for this involves spawning another “watcher” os process (one or many). This is unnecessary overhead for well-behaved programs. Moreover, the user can fix this themselves by running a script if required.
On the same note, other kill signals can be handled (such as SIGTERM). WIP changes.
akash-akya
Hi, the field name is
inputnotinput_stream. Exile is ignoringinput_streamparam. It’s hanging because there is no input.Anyway, exile should fail with proper error message for an invalid option. Thanks for pointing it out. I’ll fix this
jayjun
Thanks for investigating! 1,000 forks per second is indeed a lot of headroom.
Trade off is perfectly understandable. I specifically have trouble with this claim,
Misbehaving programs are the ones that become zombies (see
Portdocs). Exile is no different from a plain port in this respect.On the other hand, a port-based library can double as the kill-on-exit script, thus guarantee no zombies. Middleware solutions are superior here, instead of comparing with “all shells and other languages”.