Earlier quoted context omitted.
I hadn’t heard of gleam. Looks cool! I like working with elixir in a lot of ways but never was a Ruby guy, and I think I’d prefer the C-style syntax.
My current favorite language, just no time to finish my gleam projects.
Embedding Python in Elixir, it's fine
51–60 of 66 posts
Re: Embedding Python in Elixir, it's fine
#52Other commenters have already pointed out the safety implications of using NIFs for this. There are, however, other downsides worth considering: - The Erlang VM scheduler can't preempt a NIF, so a long-running Python call risks hanging the VM. This is a non-issue for ports, since Python's running in a separate OS process. A NIF can mitigate this by spawning an OS thread and yielding until it finishes; ain't clear if…
Re: Embedding Python in Elixir, it's fine
#53Earlier quoted context omitted.
> Because it uses NIFs (natively-implemented functions), an unhandled exception in Pythonx would take down your whole OS process along with all other BEAM processes, making your supervision tree a bit worthless in that regard. What's the Elixir equivalent if "Pythonic"? An architecture that allows a NIF to take down your entire supervision tree is the opposite of that, as it defeats a the stacks' philosophy. The best…
That's the thing though: a NIF execution isn't confined to the the BEAM process by its nature. From the Erlang docs: > As a NIF library is dynamically linked into the emulator process, this is the fastest way of calling C-code from Erlang (alongside port drivers). Calling NIFs requires no context switches. But it is also the least safe, because a crash in a NIF brings the emulator down too. ( https://www.erlang.org/d…
You are correct - one could still architect is such that the genserver hosting the NIF(s) run in a separate process/VM/computer in the same cluster since message passing is network-transparent, though inter-host messages have higher latencies.
Re: Embedding Python in Elixir, it's fine
#54For Livebook, this looks really cool. Love that it calls CPython directly via C++ NIFS in Elixir and returns Elixir-native data structures. That's a lot cleaner than interacting with Python in Elixir via Ports, which is essentially executing a `python` command under the hood. For production servers, Pythonx is a bit more risky (and the developers aren't claiming it's the right tool for this use case). Because it's ru…
Is there a class of exceptions that wouldn't be caught by PythonX's wrapper? FTA (with emphasis added):
> Pythonx ties Python and Erlang garbage collection, so that the objects can be safely kept between evaluations. Also, it conveniently handles conversion between Elixir and Python data structures, bubbles Python exceptions and captures standard output.
And...
> Rustler is a popular NIF wrapper for Rust in Elixir
From Rustler's Git README:
> The code you write in a Rust NIF should never be able to crash the BEAM.
I haven't used Rustler, Zigler or PythonX (yet), so I'm genuinely asking if I'm mistaken in my understanding of their safety.
Re: Embedding Python in Elixir, it's fine
#55I was super excited until I read: "...if you are using this library to integrate with Python, make sure it happens in a single Elixir process..."
Re: Embedding Python in Elixir, it's fine
#56I was super excited until I read: "...if you are using this library to integrate with Python, make sure it happens in a single Elixir process..."
What’s the problem? You plan on running multiple instances of you Elixir program?
Re: Embedding Python in Elixir, it's fine
#57Other commenters have already pointed out the safety implications of using NIFs for this. There are, however, other downsides worth considering: - The Erlang VM scheduler can't preempt a NIF, so a long-running Python call risks hanging the VM. This is a non-issue for ports, since Python's running in a separate OS process. A NIF can mitigate this by spawning an OS thread and yielding until it finishes; ain't clear if…
Re: Embedding Python in Elixir, it's fine
#58At first read this seems really promising. Getting into Elixir/Erlang ecosystem from Python has seemed too hard to take the time. And when there I wouldn't be able to leverage all the Python stuff I've learned. With Pythonx gradual learning seems now much more achievable. It wasn't mentioned in the article, but there's older blog post on fly.io [1] about live book, GPUs, and their FLAME serverless pattern [2]. Since…
Yeah, looks like it works fine, here's an example: https://pastebin.pl/view/a10aea3d I'll add: FLAME is probably a great addition to pythonx. While a NIF can crash the node it is executed on, FLAME calls are executed on other nodes by default. So a crash here would only hard-crash processes on the same node (FLAME lets you group calls so that a flame node can have many being executed on it at any time). Errors bubble…
Re: Embedding Python in Elixir, it's fine
#59At first read this seems really promising. Getting into Elixir/Erlang ecosystem from Python has seemed too hard to take the time. And when there I wouldn't be able to leverage all the Python stuff I've learned. With Pythonx gradual learning seems now much more achievable. It wasn't mentioned in the article, but there's older blog post on fly.io [1] about live book, GPUs, and their FLAME serverless pattern [2]. Since…
Yeah, looks like it works fine, here's an example: https://pastebin.pl/view/a10aea3d I'll add: FLAME is probably a great addition to pythonx. While a NIF can crash the node it is executed on, FLAME calls are executed on other nodes by default. So a crash here would only hard-crash processes on the same node (FLAME lets you group calls so that a flame node can have many being executed on it at any time). Errors bubble…
Re: Embedding Python in Elixir, it's fine
#60Earlier quoted context omitted.
Yeah, looks like it works fine, here's an example: https://pastebin.pl/view/a10aea3d I'll add: FLAME is probably a great addition to pythonx. While a NIF can crash the node it is executed on, FLAME calls are executed on other nodes by default. So a crash here would only hard-crash processes on the same node (FLAME lets you group calls so that a flame node can have many being executed on it at any time). Errors bubble…
That's a really good option - neatly sidesteps the risk of a NIF crash with practically no extra code.
But yeah if you're doing ML tasks it makes a lot of sense to farm those out to beefier or GPU-equipped nodes anyway so at that point it's just a natural synergy, AND you get the crash isolation.