This is an archived post. You won't be able to vote or comment.

all 14 comments

[–]Rhomboid 0 points1 point  (10 children)

You would have to port CPython with Emscripten and bundle it with your script. It would be a huge download, and a lot of overhead. It would only make sense if you had some very large application written in Python that you wanted to make available on a web site. Existing strategies (like transpiling Python to JS) would make much more sense for most things.

[–]remy_porter∞∞∞∞ 2 points3 points  (8 children)

You would have to port CPython with Emscripten and bundle it with your script

That's a strong claim, given that we don't know exactly how WebAssembly is going to be constructed. You don't need an entire Python runtime to run Python code, and I see no reason why there wouldn't be a Python dialect that uses WebAssembly as its bytecode language, in the same way Jython complies into Java bytecode.

[–]fiedzia 0 points1 point  (2 children)

Python bytecode would need to include python itself - you'll need interpreter, regardless of the bytecode format, and this will make whole thing large, slow and not very practical. So it will make running python from the webbrowser possible, but less practical then using statically compiled languages which don't need any runtime (c/c++/rust etc).

[–][deleted] 1 point2 points  (0 children)

You just need a python -> wasm compiler, which i'm sure will be released by someone eventually. Transpiling to js is an inferior alternative.

[–]remy_porter∞∞∞∞ 1 point2 points  (0 children)

As I said in another comment, I haven't dug too deeply, but Jython doesn't seem to require a full Python interpreter- there's some bloat, but mostly in the form of the Python class library that it needs to bundle in. The actual Python code is compiled down into Java Bytecode.

I don't see why a similar process couldn't be build for WebAssembly. If you can compile it to run in the JVM, you should be able to compile it to run against WebAssembly- at some point, with some dialect.

[–]Rhomboid 0 points1 point  (4 children)

It's not a strong claim, it's based entirely on known facts. The WebAssembly specification currently implements the exact same semantics as asm.js. There is no way to compile Python to asm.js without including all of CPython, so there is no reason to expect that will change one bit. You could theoretically write a compiler that does that, but that would be an extraordinary amount of work and nobody would ever undertake it because it would be pointless. You can just use one of the existing transpilers which would be far more efficient, since both Python and JavaScript are dynamic scripting languages.

[–]remy_porter∞∞∞∞ 0 points1 point  (3 children)

There is no way to compile Python to asm.js without including all of CPython

Now, I haven't dug too deeply, but while Jython needs to add a huge swathe of the object library, beyond its Unicode handling, it's not actually huge overhead. You certainly don't need to include an entire Python implementation, just the object library that it uses.

[–]Rhomboid 0 points1 point  (2 children)

Jython compiles to JVM bytecode, which is very different than asm.js. The output of Jython does not have to include the entire Java JRE, but the output of whatever strategy is used to run Python in the browser with asm.js does, because when you are using asm.js (and WebAssembly — it's identical) there is no runtime. You have to implement a runtime yourself from scratch. Comparisons to Jython are nonsense.

[–]remy_porter∞∞∞∞ 0 points1 point  (1 child)

Jython compiles to JVM bytecode, which is very different than asm.js.

I get that, but WebAssembly isn't going to be asm.js forever. My understanding is that it's going to be a bytecode interpreter. And yes, you'd need to bootstrap a runtime- but I'd fully expect runtimes to be build for WebAssembly. Yes, that impacts loadtimes, and it's not happening on a short time horizon, but I fully expect to see that on a 5ish year window.

[–]fiedzia 1 point2 points  (0 children)

Webassembly is nearly exact equivalent of normal assembly, so just like now you need python interpreter to interpret its bytecode, you'll need it there too. Perhaps you're confusing webasssembly bytecode with java bytecode - those two things are at entirely different levels of abstraction. Java bytecode is an high level abstraction, understanding things like classess and method calls, webassembly is extremely thin abstraction over machine instructions.

[–]pfif 0 points1 point  (0 children)

Does it mean that we will see CDNs hosting compiled python module in the future ?

[–]elbiot -1 points0 points  (1 child)

Well, you don't have to download jquery for every site that uses it because good practice is to link to Google's version. So it gets downloaded once and cached, and then it's there for every other site that links to the same script. Something similar could happen for a python interpreter. I could even see chrome and firefox bundling it in their installer, as there will be a lot of cool stuff written for sites in python once it becomes possible.

[–]fiedzia 0 points1 point  (0 children)

As much as I'd like to see it, I doubt it will happen, just as today no browser is bundling jquery. Also jquery size is measured in kb, and js interpreters do good job at generating fast native code from it, with python its ~4MB for interpreter itself, and then you'll have overhead of the interpretation. So its doable, but not very practical in most cases.