by thecity2 · 9y ago · 0 comments
You'd have to create a Frankenstein interpreter to get anything working.
The gains you get for free using otp and the beam vm depend heavily around language integration of vm features. Porting python to a vm it would work slowly on and would be unable to use most of, if not all of, the benefits without changing the language would give you what exactly?
"Mochi is a dynamically typed programming language for functional programming and actor-style programming. Its interpreter is written in Python3. The interpreter translates a program written in Mochi to Python3's AST / bytecode."
If all that can be done in Python 3, surely it could be ported to run on BEAM to good effect.
What couldn't you do, say by making BEAM/OTP -specific features be a library you import in Python, and by mapping Python language features to however Erlang does them?
I get there will likely be problems, but what specific problems would you run in to?
Python has no concept of a process like Erlang does. The standard library does even know about, let alone is able to deal with, the difference between cast and call messages. It can't do messages even. There is no way to translate monitors vs links. What's a node? .....
You could implement python syntax maybe, but python will never make sense on beam.
I don't know that Python on the beam is a good idea / worth the work, but I don't think there's a fundamental issue here:
A process is more or less a thread, which Python already knows about.
Cast is just sending a message, which is erlang:msg(Dest, Msg); call is send a message plus selective receive. General receive is easy, call a function (not already available) and return the message. Selective receive is trickier, because python doesn't have a construct for matching like Erlang, so you'd have to figure out how to specify the possible message signatures you'd like to receive; plus extra work to get the make_ref mailbox marking optimization.
Monitors and links are just calling functions, and maybe being killed or getting specific messages later -- it's a VM feature, not a language feature. Same with a node.
You would have to make some hard decisions about any pythonic state though. It would be more functionally pure to pass that around as a function argument, but it might feel more like python if it was stuffed into the process dictionary. Python state shared between multiple processes/threads would be forbidden, of course. Mutability would also need some soul searching to fit into Beam's garbage collection.
EDIT to add: of course, you would need to restrict I/O to message passing -- anything with file descriptors should be rewritten to be message passing with beam ports.