singlestore-labs / python-wasi

Utilities for building CPython for the WASI platform
Apache License 2.0
97 stars 14 forks source link

Can wasmtime loads the python3.wasm in the process, and feed multiple python scripts in it? #9

Closed Yifei-yang7 closed 1 year ago

Yifei-yang7 commented 1 year ago

Currently I can run a single python script on the wasm-compiled interpreter, e.g. wasmtime run --mapdir=/opt/wasi-python/lib/python3.12::/opt/wasi-python/lib/python3.12 -- /opt/wasi-python/bin/python3.12.wasm -c "$(cat $(pwd)/test.py)". Is there a way to run multiple scripts with this (i.e. without loading the wasm-compiled interpreter multiple times)? Thanks!

kesmit13 commented 1 year ago

You have a couple of options. You could map a directory to where your files are then execute them as follows:

exec(compile(open('/path/to/files/file1.py').read(), file, 'exec'))

Another option would be to map a directory, then add that directory to your Python import path and import each one.

import sys
sys.path.insert(0, '/path/to/files')

import file1
import file2

If you don't want to map directories, you can use wasi-vfs to embed the files into the Python Wasm file and use the above techniques. The second technique should work with pyc files as well (for issue #10).

Yifei-yang7 commented 1 year ago

It seems to be related to --mapdir=/opt/wasi-python/lib/python3.12::/opt/wasi-python/lib/python3.12, may I know what's the meaning of these two directory paths? If I want to map to my files, which one should I change?

kesmit13 commented 1 year ago

That option is mapping a local directory to a directory in the Wasm runtime so that the files can be accessed by Python. If you are using the latest version of this project, you don't even need this option anymore because the Python standard library is embedded in the Wasm file. You can add your own directory mappings using additional --mapdir= options to the wasmtime call so that you can access directories where you can access your own Python files.

Yifei-yang7 commented 1 year ago

Thanks!