파이썬과 웹 어셈블리 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

 

 

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
22 [unity, unity3d] 씬 뷰 조작법 | 유니티 졸리운_곰 2024.12.26 198
21 [Unity] 3D 씬(Scene) 뷰 조작법 file 졸리운_곰 2024.12.23 220
20 [Unity] 3D Unity 조작하기 file 졸리운_곰 2024.12.23 236
19 [Unity] [모바일, 2D] 유니티 기초2 - 씬 전환하기 file 졸리운_곰 2024.12.14 186
18 [Unity] How to Create a Metaverse dApp with Unity : Unity로 Metaverse dApp을 만드는 방법 file 졸리운_곰 2023.09.13 397
17 [Unity] [유니티 게임개발] [Unity] ML-Agents 설치 및 테스트해보기 file 졸리운_곰 2023.01.05 223
16 [Unity] [Unity2D] 유니티 2D기본과 애니메이션 file 졸리운_곰 2022.12.19 208
15 [Unity] Unity 3D Keyframe Animation 유니티 애니메이션 file 졸리운_곰 2022.12.19 182
14 [unity] 유니티 에셋스토어에서 구매한 에셋 불러오기 file 졸리운_곰 2022.12.04 327
13 Calling RESTful APIs in Unity3D file 졸리운_곰 2021.01.02 268
12 Unity C# – Coroutine 알아보기 file 졸리운_곰 2020.12.09 228
11 How to Make a Mini Minecraft Game file 졸리운_곰 2020.07.20 344
10 Coding your own blockchain with Unity3D file 졸리운_곰 2020.01.02 365
9 [Unity] ML-Agent 설치 및 실행 file 졸리운_곰 2019.08.15 621
8 유니티 머신러닝 에이전트 소개 file 졸리운_곰 2019.08.15 694
7 블록체인과 유니티3d : Coding your own blockchain with Unity3D file 졸리운_곰 2019.02.22 536
6 Reflection of Unity C# file 졸리운_곰 2017.06.25 628
5 [유니티 C# 기초 강의] 12. 델리게이트 file 졸리운_곰 2017.06.25 398
4 유니티에서 턴제 게임 구현하기 졸리운_곰 2017.05.09 4621
3 Unity3D 씬 뷰 file 졸리운_곰 2017.04.23 678
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED