Skip to content

Commit b434d95

Browse files
authored
Updates for latest Faasm (#12)
* Update to removal of shared_store dir * Adding remote upload for funds * Add invoke and upload functions * Add logging for local function upload * Fix printing error
1 parent 0e884d9 commit b434d95

File tree

2 files changed

+79
-31
lines changed

2 files changed

+79
-31
lines changed

README.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Faasm Python Environment [![Tests](https://github.com/faasm/python/workflows/Tests/badge.svg?branch=master)](https://github.com/faasm/python/actions) [![License](https://img.shields.io/github/license/faasm/python.svg)](https://github.com/faasm/python/blob/master/LICENSE.md)
1+
# Faasm Python Environment [![Tests](https://github.com/faasm/python/workflows/Tests/badge.svg?branch=master)](https://github.com/faasm/python/actions) [![License](https://img.shields.io/github/license/faasm/python.svg)](https://github.com/faasm/python/blob/master/LICENSE.md)
22

33
This build cross-compiles CPython and a number of Python modules to WebAssembly
44
for use in [Faasm](https://github.com/faasm/faasm).
@@ -10,9 +10,9 @@ interface](https://github.com/faasm/faasm/blob/master/docs/host_interface.md).
1010
## Set-up and development
1111

1212
This repo is developed using the Faasm development environment, set up according
13-
to [the docs](https://github.com/faasm/faasm/blob/master/docs/development.md).
13+
to [the docs](https://github.com/faasm/faasm/blob/master/docs/development.md).
1414

15-
To set up your local environment, run the `python` CLI as per the Faasm docs,
15+
To set up your local environment, run the `python` CLI as per the Faasm docs,
1616
then:
1717

1818
```bash
@@ -42,7 +42,7 @@ inv runtime
4242
inv func
4343

4444
# Copy the actual Python functions into place
45-
inv func.uploadpy --local
45+
inv func.upload-all --local
4646
```
4747

4848
## Code changes
@@ -56,10 +56,10 @@ compare](https://github.com/python/cpython/compare/v3.8.2...faasm:faasm).
5656
A similar (small) list of changes for numpy can be seen
5757
[here](https://github.com/numpy/numpy/compare/v1.19.2...faasm:faasm).
5858

59-
CPython is built statically, some notes on this process can be found
60-
[here](https://wiki.python.org/moin/BuildStatically).
59+
CPython is built statically, some notes on this process can be found
60+
[here](https://wiki.python.org/moin/BuildStatically).
6161

62-
Several of the code changes to CPython and numpy were borrowed from
62+
Several of the code changes to CPython and numpy were borrowed from
6363
[pyodide](https://github.com/iodide-project/pyodide).
6464

6565
## Releasing
@@ -107,7 +107,7 @@ scripts on the build machine (not any other Python that might be installed).
107107

108108
Do **not** upgrade Pip in the build machine copy of Python.
109109

110-
The versions of Pip and Python for wasm and the build machine must match
110+
The versions of Pip and Python for wasm and the build machine must match
111111
exactly.
112112

113113
### Building CPython to WebAssembly
@@ -121,10 +121,10 @@ inv cpython
121121
The result is installed at `third-party/cpython/install/wasm`.
122122

123123
We provide a [Setup.local](third-party/cpython/Modules/Setup.local) file, which
124-
specifies which standard CPython modules will be built statically.
124+
specifies which standard CPython modules will be built statically.
125125

126126
At the end of the CPython build, it will print out a list of which modules have
127-
been successfully built and which have failed. Note that many of the standard
127+
been successfully built and which have failed. Note that many of the standard
128128
library modules will fail in this build, but the ones we need should succeed.
129129

130130
## Cross-compilation set-up
@@ -137,13 +137,13 @@ See the dev instructions above for set-up.
137137

138138
### Changing the crossenv environment
139139

140-
Crossenv picks up the cross-compilation environment from the CPython
141-
build artifacts. Therefore, to make changes to the cross-compilation
140+
Crossenv picks up the cross-compilation environment from the CPython
141+
build artifacts. Therefore, to make changes to the cross-compilation
142142
environment:
143143

144144
- Modify the CPython build (see `tasks.py`)
145-
- Rerun the CPython build (`inv cpython --clean`)
146-
- Rebuild the crossenv (`./bin/crossenv_setup.sh`)
145+
- Rerun the CPython build (`inv cpython --clean`)
146+
- Rebuild the crossenv (`./bin/crossenv_setup.sh`)
147147
- Enter the crossenv and inspect the environment with `bin/sanity_check.py`
148148

149149
## Modules
@@ -155,7 +155,7 @@ To run this you must first have the cross-env activated as described above.
155155

156156
```bash
157157
# Install all supported modules
158-
inv libs.install
158+
inv libs.install
159159

160160
# Install experimental modules
161161
inv libs.install --experimental
@@ -167,7 +167,7 @@ inv libs.install --name numpy
167167
inv libs.install --name <module_name>
168168
```
169169

170-
Libraries will then be installed to
170+
Libraries will then be installed to
171171
`cross_venv/cross/lib/python3.8/site-packages`.
172172

173173
### Debugging module builds
@@ -206,7 +206,7 @@ inv libs.install --name mxnet
206206
inv libs.install --name horovod
207207
```
208208

209-
### Cleaning and uninstalling
209+
### Cleaning and uninstalling
210210

211211
```
212212
# Clean then rebuild

tasks/func.py

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from faasmtools.compile_util import wasm_cmake
22
from faasmtools.env import WASM_DIR
3+
from faasmtools.build import FAASM_LOCAL_DIR
4+
5+
import requests
36
from os.path import join
47
from invoke import task
5-
from faasmtools.build import FAASM_LOCAL_DIR
68
from os import makedirs, listdir
79
from shutil import copy
810
from tasks.env import PROJ_ROOT
@@ -17,7 +19,7 @@
1719
DEST_FOLDER = join(WASM_DIR, USER, FUNC)
1820
DEST_FILE = join(DEST_FOLDER, "function.wasm")
1921

20-
FAASM_SHARED_STORAGE_ROOT = join(FAASM_LOCAL_DIR, "shared_store")
22+
FAASM_SHARED_STORAGE_ROOT = join(FAASM_LOCAL_DIR, "shared")
2123
PY_FUNC_DIR = join(PROJ_ROOT, "func", "python")
2224
PY_UPLOAD_DIR = join(FAASM_SHARED_STORAGE_ROOT, "pyfuncs", "python")
2325

@@ -31,30 +33,76 @@ def compile(ctx, clean=False, debug=False):
3133
wasm_cmake(FUNC_DIR, FUNC_BUILD_DIR, FUNC, clean, debug)
3234

3335
# Copy the wasm into place
34-
3536
makedirs(DEST_FOLDER, exist_ok=True)
3637
copy(WASM_FILE, DEST_FILE)
3738

3839

3940
@task
40-
def uploadpy(ctx, local=False):
41+
def upload(ctx, host="upload", port=8002):
4142
"""
42-
Upload functions written in Python
43+
Upload the CPython function
4344
"""
44-
if not local:
45-
raise RuntimeError("Remote upload not yet implemented")
45+
url = "http://{}:{}/f/{}/{}".format(host, port, USER, FUNC)
46+
print("Uploading {}/{} to {}".format(USER, FUNC, url))
47+
response = requests.put(url, data=open(WASM_FILE, "rb"))
4648

47-
makedirs(PY_UPLOAD_DIR, exist_ok=True)
49+
print("Response {}: {}".format(response.status_code, response.text))
4850

49-
# Get all Python funcs
50-
funcs = listdir(PY_FUNC_DIR)
51-
funcs = [f for f in funcs if f.endswith(".py")]
52-
funcs = [f.replace(".py", "") for f in funcs]
5351

54-
for func in funcs:
52+
@task
53+
def uploadpy(ctx, func, local=False, host="upload", port=8002):
54+
src_file = join(PY_FUNC_DIR, "{}.py".format(func))
55+
56+
if local:
57+
makedirs(PY_UPLOAD_DIR, exist_ok=True)
5558
func_upload_dir = join(PY_UPLOAD_DIR, func)
5659
makedirs(func_upload_dir, exist_ok=True)
5760

58-
src_file = join(PY_FUNC_DIR, "{}.py".format(func))
5961
dest_file = join(func_upload_dir, "function.py")
62+
print("Copying function {} {} -> {}".format(func, src_file, dest_file))
6063
copy(src_file, dest_file)
64+
else:
65+
url = "http://{}:{}/p/{}/{}".format(host, port, "python", func)
66+
response = requests.put(url, data=open(src_file, "rb"))
67+
68+
print(
69+
"{} response {}: {}".format(
70+
func, response.status_code, response.text
71+
)
72+
)
73+
74+
75+
@task
76+
def upload_all(ctx, local=False, host="upload", port=8002):
77+
"""
78+
Upload all Python functions
79+
"""
80+
funcs = listdir(PY_FUNC_DIR)
81+
funcs = [f for f in funcs if f.endswith(".py")]
82+
funcs = [f.replace(".py", "") for f in funcs]
83+
84+
for func in funcs:
85+
uploadpy(ctx, func, local=local, host=host, port=port)
86+
87+
88+
@task
89+
def invoke(ctx, user, func, input_data=None, host="worker", port=8080):
90+
"""
91+
Invoke a function
92+
"""
93+
94+
url = "http://{}:{}".format(host, port)
95+
data = {
96+
"user": USER,
97+
"function": FUNC,
98+
"py_user": user,
99+
"py_func": func,
100+
"python": True,
101+
}
102+
103+
if input_data:
104+
data["input_data"] = input_data
105+
106+
response = requests.post(url, json=data)
107+
108+
print("Response {}:\n{}".format(response.status_code, response.text))

0 commit comments

Comments
 (0)