파이썬과 웹 어셈블리 Python and WebAssembly

Python and WebAssembly

Despite its name, WebAssembly is not limited to the browser. In fact, it's potential outside of the browser is at least as interesting. In this post I talk about some experimental work that we have done in combining WebAssembly with Python.


Edinburgh - Image by Almar Klein (CC BY 4.0)

Introduction

In my previous post, I argue that WebAssembly would be a very interesting target for (new) programming languages that are intended to run fast, and run "anywhere". Not long after that publication I gave a talk at EuroScipy 2017 about WebAssembly. At the same conference, Windel Bouwman gave a talk about his PPCI project: a compiler infrastructure written in Python. The two of us teamed up, and in the train on the way back we managed to create a prototype in which Python consumed WebAssembly code and ran it.

In the months that followed, we've continued working on it, and this culminated in a talk at EuroPython 2018, in which we show what we had achieved. In this post I dive into some of the details.

WebAssembly is not only for the web!

Although WebAssembly is primarily designed for the web, its use outside of the web has been taken into account from the beginning. And it has great potential there ...

For one, WebAssembly is a well-designed open standard for fast executable code. Let this simple fact sink in; once we have good WebAssembly runtimes on all major operating systems, we can build true cross-platform binaries! I also don't expect it to take long before mobile devices natively support WebAssembly.

Since WebAssembly is well-defined and makes few assumptions about the underlying hardware, it is excellent for taking on the role of an intermediate representation (IR). For instance, compile language X to WebAssembly, and then compile the WebAssemby to machine code for an embedded device (e.g. Xtensa).

Further, every WebAssembly module very clearly specifies what it needs, and only gets what it needs when the host environment provides it. This means that users can potentially have a lot of control and insight into what an application can do on your system. This feature is critical in the browser, but about as important on mobile (i.e. app permissions), and actually, isn't it odd that we don't have this on desktop too?

How can Python use WebAssembly?

For most compiled languages (e.g. Rust) the advantage of adopting WebAssembly is pretty simple: compile to WebAssembly instead of the normal compile target, and now you can run it in a browser.

Python is not a compiled language, which means that things are not so straightforward. But this does not mean that the Python community cannot make use of WebAssembly. I will discuss three potential uses below.

Compile a Python interpreter

One of the more obvious use-cases is to take a Python interpreter, compile it to WebAssembly, and voila, you have a Python interpreter in the browser!

This has several interesting and useful use-cases. But would I use this approach to create a web application? Probably not; the WebAssembly code that needs to be loaded (the Python runtime) would be pretty big, and the code would not be faster than the original runtime.

Examples that use this approach:

  • Pyodide compiles CPython, Numpy and a few more scientific packages to WebAssembly to realize a scientific Python stack in the browser!
  • PypyJS Takes the PyPy interpreter and compiles it to WebAssembly (technically it uses ASM, say, the precursor of WebAssembly).
  • RustPython (by Windel) is a new Python interpreter written in Rust. And since Rust has excellent tooling to compile to WebAssembly ...

Compile a subset of Python

In PPCI we've created a compiler that statically compiles a subset of Python to WebAssembly. It is only capable of compiling a very strict subset of Python. For instance, it assumes that all objects are floating point numbers. This approach could be improved, e.g. by supporting more types using Python's type hints. However, the supported subset of Python will always be limited to something that can be statically compiled.

Despite this restriction, an approach like this could be very useful to write functions that can be used inside a web application. Or ... to write code that runs at native speed. The example below shows the resemblance with Numba:

@wasm.wasmify
def find_prime(nth):
    n = 0
    i = -1       
    while n < nth:
        i = i + 1        
        if i <= 1:
            continue  # nope
        elif i == 2:
            n = n + 1
        else:
            gotit = 1
            for j in range(2,  i//2+1):
                if i % j == 0:
                    gotit = 0
                    break
            if gotit == 1:
                n = n + 1    
    return i

In the talk I show a demo of this, and yes, the code indeed runs much faster than plain Python!

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

It should be noted though, that the speed at which code like this can compile and run is highly dependent on the WebAssembly compiler (the part that compiles WebAssembly to native machine code). We're now doing this in pure Python with PPCI, which makes the compilation slow, and it does not perform many optimizations.

In comparison, Firefox does amazing things to compile WebAssembly modules. Maybe it's possible to leverage Mozilla's efforts and use its WebAssembly compiler on desktop, from Python?

Python as a WebAssembly platform

WebAssembly is a modern standard and is very well designed overall. For example, a WebAssembly module very explicitly defines its imports (the things that it needs), and the exports (the things that it provides). It is up to the host environment (e.g. JavaScript) to provide the imports and make use of the exports.

In PPCI, we've created tooling to load binary WebAssembly modules and examine their imports and exports. Using an API very similar to that of JavaScript, we've made it possible to provide imports in the form of Python functions. ... yes, you can call back into Python functions from WebAssembly code that is running natively!

In that way, it is possible to take multiple WebAssembly modules—which can be created with any language—and bind them together in Python. This is what Python has always been good at: binding things together. But now the things are self-contained modules that can each be written in another language.

To illustrate this idea, we took the WebAssembly module of the Rocket game. We did not read any docs, we just took the binary WebAssembly module and loaded it into PPCI. We examined the imports and exports and that was all we needed to write the supporting Python code to make the game work.

Next, we wrote an AI that is capable to play the game automatically. We wrote this in C, which we compiled to WebAssembly. Now we had two WebAssembly modules, which we bind together in Python to create a complete system of an AI playing the Rocket game :)

Wrapping up

 

WebAssembly is coming and it's awesome. It definitely has its use on desktop too, and there are several ways in which the Python community can make use of it!

[출처] https://almarklein.org/python_and_webassembly.html

 

 

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
71 무작정 배우는 C언어 머드(MUD)게임 만들기 Part4 게임스랩 file 졸리운_곰 2020.10.17 563
70 무작정 배우는 C언어 머드(MUD)게임 만들기 Part3 게임스랩 file 졸리운_곰 2020.10.17 1020
69 무작정 배우는 C언어 머드(MUD)게임 만들기 Part2 게임스랩 file 졸리운_곰 2020.10.17 1236
68 무작정 배우는 C언어 머드(MUD)게임만들기 Part1 게임스랩 file 졸리운_곰 2020.10.17 5646
67 [python] [ pyGame] 컴퓨터그래픽스 : 그래픽 프리미티브 그리기 file 졸리운_곰 2020.08.09 397
66 How to Make a Mini Minecraft Game file 졸리운_곰 2020.07.20 344
65 Learn to Develop Alphabet Learning Mobile Game for Kids using Mobile Blazor Bindings file 졸리운_곰 2020.07.04 260
64 Coding your own blockchain with Unity3D file 졸리운_곰 2020.01.02 365
63 Cheat Engine source 게임 해킹 치트엔진 소스 file 졸리운_곰 2019.12.28 461
62 2019년과 이후 JavaScript의 동향 - WebAssembly file 졸리운_곰 2019.12.19 290
61 Compiling a New C/C++ Module to WebAssembly 졸리운_곰 2019.12.19 363
60 [개발바보]WebAssembly, 브라우저에 올리는 네이티브 코드 file 졸리운_곰 2019.12.19 278
59 Rust 코드를 웹 (어셈블리)로 전환 : Compiling from Rust to WebAssembly 졸리운_곰 2019.12.19 364
58 WebAssembly 101: a developer's first steps file 졸리운_곰 2019.12.19 455
57 짱! 웹 어셈블리 문서 링크 Awesome Wasm file 졸리운_곰 2019.12.19 535
56 웹어셈블리의 컨셉, 어셈블리가 뭔가요? file 졸리운_곰 2019.12.18 583
» 파이썬과 웹 어셈블리 Python and WebAssembly file 졸리운_곰 2019.12.18 1663
54 [Web Assembly] Exploring Blazor with Visual Studio 2019 file 졸리운_곰 2019.12.18 6597
53 [Unity] ML-Agent 설치 및 실행 file 졸리운_곰 2019.08.15 621
52 유니티 머신러닝 에이전트 소개 file 졸리운_곰 2019.08.15 694
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED