Skip to content

Instantly share code, notes, and snippets.

@pims
Last active January 3, 2024 20:26
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pims/711549577759ad1341f1a90860f1f3a5 to your computer and use it in GitHub Desktop.
Save pims/711549577759ad1341f1a90860f1f3a5 to your computer and use it in GitHub Desktop.
Example of running a python script via python-wasi using the wasmtime wasm runtime
print("hello from a python script")
hello from a python script
from wasmtime import Config, Engine, Linker, Module, Store, WasiConfig
def main():
cfg = Config()
# necessary if we want to interrupt execution after some amount of instructions executed
cfg.consume_fuel = True
cfg.cache = True
engine = Engine(cfg)
linker = Linker(engine)
linker.define_wasi()
"""
from https://wasmlabs.dev/articles/python-wasm32-wasi/
wget https://github.com/vmware-labs/webassembly-language-runtimes/releases/download/python%2F3.11.1%2B20230118-f23f3f3/python-aio-3.11.1.zip
unzip python-aio-3.11.1.zip
rm python-aio-3.11.1.zip
tree
.
├── app.py [new]
├── bin
│ ├── python-3.11.1-wasmedge.wasm
│ └── python-3.11.1.wasm
├── runner.py [this file]
└── usr
└── local
└── lib
├── python3.11
│ ├── lib-dynload
│ └── os.py
└── python311.zip
"""
module = Module.from_file(linker.engine, "bin/python-3.11.1.wasm")
config = WasiConfig()
"""
cat app.py
print("hello from a python script")
"""
config.argv = ("python", "app.py")
config.preopen_dir(".", "/")
# mkdir chroot, which is an arbitrary name for a directory
config.stdout_file = 'chroot/out.log' # will be created if it doesn't exist
config.stderr_file = 'chroot/err.log' # will be created if it doesn't exist
store = Store(linker.engine)
store.add_fuel(500_000_000) # amount of fuel limits how many instructions can be executed
store.set_wasi(config)
instance = linker.instantiate(store, module)
# _start is the default wasi main function
start = instance.exports(store)["_start"]
mem = instance.exports(store)["memory"]
mem_size = mem.size(store)
data_len = mem.data_len(store)
print(f"mem.size: {mem_size} pages of 64kb")
print(f"mem.data_len: {data_len:_}")
start(store)
consumed = store.fuel_consumed()
print(f"fuel consumed: {consumed:_}")
with open('chroot/out.log') as f:
print(f.read())
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment