Live data from Hacker News

Embedding Python in Elixir, it's fine

dashbit.co

51–60 of 66 posts

Re: Embedding Python in Elixir, it's fine

#51
post #40

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.

Ah, projects. Ain’t it always the way.

Re: Embedding Python in Elixir, it's fine

#52

Other 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…

I would have guess the builders of this would have mitigated this problem by running the python in a thread? That won't hang the VM (or cause a segfault at the 1ms boundary). It might cause OS starvation in extreme cases, but you'd have to be really extreme.

Re: Embedding Python in Elixir, it's fine

#53
post #38

Earlier 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…

> The emulator in this context is the BEAM VM that is running the whole application (including the supervisors)

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

#54
post #13

For 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…

> an unhandled exception in Pythonx would take down your whole OS

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

#56

I 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?

I'm using Python script in my Phoenix app (not Livebook). I hoped Pythonx would solve all the issues associated with System.cmd, but as you can imagine, I have more than one user.

Re: Embedding Python in Elixir, it's fine

#57

Other 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…

These are excellent points.

Re: Embedding Python in Elixir, it's fine

#58

At 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…

That's a really good option - neatly sidesteps the risk of a NIF crash with practically no extra code.

Re: Embedding Python in Elixir, it's fine

#59

At 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…

Well this seems nice and easy. Thank you for the example. There's local, Kubernetes and fly.io support for FLAME (that I found after short search). I envision running main Erlang VM on a light weight server continuously, and starting beefier machines for Python tasks as needed.

Re: Embedding Python in Elixir, it's fine

#60

Earlier 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.

You do still need some infrastructure - the Flame LocalBackend is for dev mostly and I'm pretty sure it just runs in the same vm as the parent.

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.

Post reply on HN