GraalVM’s implementation of Python is in the early stages of development. A primary goal is to support SciPy and its constituent libraries, but we have a long way to go there. At this point, the Python implementation is made available for experimentation and curious end-users. GraalVM currently aims to be compatible with Python 3.7, but it is a long way from there, and it is very likely that any Python program that requires any imports at all will hit something unsupported.
A great visualization of what running Python on GraalVM means in terms of underlying components is shown below – I have ‘borrowed’ this illustration from the article Introduction to the Python implementation for GraalVM by Franzi Geiger on Medium (August 2019) Python code is parsed into a Truffle Abstract Syntax Tree represented in Java Byte Code. Any non-JVM language running GraalVM is parsed into such an AST. GraalVM’s runtime knows how to run these trees. And the polyglot glue in GraalVM knows how to merge trees before executing them.
The sample Python application demonstrates a number of aspects of polyglot interaction from Python:
- how to execute a function object returned from another language
- how to put objects – data objects and functions – into the Polyglot map that lives between all polyglot languages executed on GraalVM
- how to retrieve objects from the Polyglot map – read and write them or execute them
In each case, a snippet of code is evaluated at runtime and the result of the evaluation against the proper language engine is returned to the Python program, ready for inspection and further manipulation.
Java from Python
Java interoperability works a little differently from the polyglot interaction with non-JVM (or Truffle) languages. Java Classes – custom and 3rd party libraries – are provided on the classpath parameter passed to graalpython. All standard Java 8 APIs are available at all times.
The output from this specific Python code fragment
Here is the code for the complete application:
To run the entire application on GraalVM:
graalpython --polyglot --jvm ./python_polyglot.py
GraalPython is the Python runtime execution engine on GraalVM. The polyglot setting indicates that polyglot language interoperability is active and the jvm switch is currently required for using the JVM as execution platform: To interoperate with other languages, we had to supply the –jvm argument above. This instructs the launcher to run on the JVM instead of in the Native Image mode – you will notice a longer startup time than with the native image graalpython execution engine (substantially longer).
If we want to invoke Java Objects defined outside the Java 8 APIs, we have to provide them to Graalpython by specifying the –vm.cp=<colon separated list of JAR Files, directories> command line option.
The output of running the application:
GitHub Repository with sources for this article: https://github.com/AMIS-Services/jfall2019-graalvm
GraalVM Docs on Graal Python – https://www.graalvm.org/docs/reference-manual/languages/python/
GraalVM Docs – interoperability from Python – https://www.graalvm.org/docs/reference-manual/languages/python/#interoperability
GraalVM Docs – Polyglot including Python as target and as starting language: https://www.graalvm.org/docs/reference-manual/polyglot/
GitHub Home of GraalPython – https://github.com/graalvm/graalpython
Introduction to the Python implementation for GraalVM – https://medium.com/graalvm/how-to-contribute-to-graalpython-7fd304fe8bb9
JavaDocs for GraalVM Polyglot – https://www.graalvm.org/truffle/javadoc/org/graalvm/polyglot/package-summary.html