Python

Faasm executes functions compiled to WebAssembly, which therefore rules outlanguages that cannot be compiled to WebAssembly. However, we can supportdynamic languages like Python by compiling the language runtime to WebAssembly.

In Faasm we do this with Python via a set of custom C-extensions and decorators to support theFaasm host interface provided in Pyfaasm,and use CPython compiled to WebAssembly with Pyodide.

Enabling Python support

Python support is not enabled by default. To enable the Python runtime you must set up the relevantenvironment variables:

  1. # On the "upload" container/ endpoint (see docker-compose.yml locally)
  2. PYTHON_CODEGEN=on

The first time the system runs it will generate machine code for CPython and all thePython C-extensions. This can take around a minute.

Running a Python function

An example Python function is found at func/python/hello.py. This can be uploadedand invoked from the Faasm CLI with:

  1. inv upload python hello --py
  2. inv invoke python hello --py

This should give a message and the version of Python being run.

Python API

A simple example of chaining Python functions in Faasm looks like:

  1. from pyfaasm.code import await_call, chain_this, faasm_func, faasm_main
  2.  
  3. @faasm_func(1)
  4. def func_one():
  5. pass
  6.  
  7. @faasm_func(2)
  8. def func_two():
  9. pass
  10.  
  11. @faasm_main
  12. def main_func():
  13. call_one = chain_this(1)
  14. call_two = chain_this(2)
  15.  
  16. await_call(call_one)
  17. await_call(call_two)

Building CPython

Note that this is only relevant for building the Python support from scratch.

Python 3.7

We need a native version of exactly the right Python 3.7, not necessarily the latest.You need to install it with the python3_7.yml playbook. It will install a copy of pythonat /usr/local/faasm/python3.7

  1. cd ansible
  2. ansible-playbook python3_7.yml

Building CPython and packages

You must make sure that you've set up the latest toolchain, build libc and malloc etc. (see local dev docs).

Once this is done, you can run the following:

  1. cd third-party/pyodide
  2. source workon.sh
  3. cd cpython
  4. make clean && make
  5. cd ../packages
  6. make clean && make

Setting up the runtime environment

Once this is all built we can put the relevant files in place in a new terminal session at the root of the project:

  1. inv python.set-up-runtime

Adding packages

Adding packages to Pyodide is described in their docs. For just a pure Python package you can do the following:

  1. cd third-party/pyodide
  2. source workon.sh
  3. ./bin/pyodide mkpkg <pypi pkg name>

If this doesn't work you can do the following:

  • Create a new folder in pyodide/packages
  • Copy the meta.yml from another pure Python package (e.g. perf)
  • Add the right version, SHA and a link to the .tar.gz from PyPI (perf example here)
  • From the packages directory run ../bin/pyodide buildpkg —package_abi=0 <your_pkg>/meta.yaml

This will automatically create a basic meta.yml.

You can then build it with:

  1. ./bin/pyodide buildpkg --package_abi=0 packages/<pypi pkg name>/meta.yaml

Adding the new package to the runtime root

Once added, we need to include this in the Faasm runtime root:

  • Open tasks/python.py
  • Add a new entry in the dictionary of Python packages in there
  • Make sure you use the right file path to the built package in pyodide

You can then set it up with:

  1. inv python.set-up-package <pkg name>

Note that this modifies the runtime root bundle included in a release, so changes here will need tobe reflected in a new release.