Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Yifei-yang7 opened this issue Mar 5, 2023 · 4 comments

Comments

@Yifei-yang7
Copy link

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
Copy link
Collaborator

kesmit13 commented Mar 5, 2023

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
Copy link
Author

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
Copy link
Collaborator

kesmit13 commented Mar 6, 2023

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
Copy link
Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants