You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
### Does module/package XYZ work on GraalVM's Python implementation?
3
+
### Does module/package XYZ work on GraalVM's Python runtime?
4
4
5
-
It depends, but is currently unlikely. The first goal with the GraalVM Python implementation was to
6
-
show that NumPy and related packages can run using the managed GraalVM LLVM
7
-
implementation. Now the GraalVM team continue to improve the
8
-
number of passing CPython unittests and to track the compatibility with popular PyPI packages.
5
+
It depends, but is currently unlikely.
6
+
The first goal with GraalVM's Python implementation was to show that NumPy and related packages can run using the managed GraalVM LLVM runtime.
7
+
The GraalVM team continues to improve the number of passing CPython unittests, and to track the compatibility with popular PyPI packages.
9
8
10
9
### Can the GraalVM Python implementation replace my Jython use case?
11
10
12
-
It can, but there are some caveats, like Python code subclassing Java
13
-
classes or use through the `javax.script.ScriptEngine` not being
14
-
supported. See the [Jython Compatibility](Jython.md) guide for details.
11
+
It can, but there are some caveats, like Python code subclassing Java classes or use through the `javax.script.ScriptEngine` not being supported.
12
+
See the [Jython Compatibility](Jython.md) guide for details.
15
13
16
-
### Do I need to compile and run native modules as LLVM bitcode to use GraalVM's Python implementation?
14
+
### Do I need to compile and run native modules as LLVM bitcode to use on GraalVM's Python runtime?
17
15
18
-
If you want to run C extensions or use certain built-in features, yes, you need
19
-
to build the module with GraalVM's Python and then it will run using the GraalVM
20
-
LLVM runtime. However, many of the core features of Python (including e.g.,
21
-
large parts of the `os` API) are implemented in pure Java and many standard
22
-
library modules and packages work without running any LLVM bitcode. So even
23
-
though GraalVM's Python depends on the GraalVM LLVM runtime, for many use cases
16
+
If you want to run C extensions or use certain built-in features, yes, you need to build the module with GraalVM's Python implementation, and then it will run using the GraalVM LLVM runtime.
17
+
However, many of the core features of Python (including, e.g., large parts of the `os` API) are implemented in pure Java and many standard library modules and packages work without running any LLVM bitcode.
18
+
So even though the Python implementation depends on the GraalVM LLVM runtime, for many use cases
24
19
you can disallow native modules entirely.
25
20
26
-
### Can I use GraalVM sandboxing features with GraalVM's Python implementation?
21
+
### Can I use the GraalVM sandboxing features with Python?
27
22
28
-
Yes, you can. As an embedder, you can selectively disable features. For example, you
29
-
can disable native code execution or filesystem access. If you are a user of
30
-
Oracle GraalVM Enterprise Edition, you will also find that the managed execution mode
31
-
for LLVM fully works for running extensions such as NumPy in a safer manner.
23
+
Yes, you can.
24
+
As an embedder, you can selectively disable features.
25
+
For example, you can disable native code execution or filesystem access.
26
+
Also, GraalVM's managed execution mode for LLVM fully works for running extensions such as NumPy in a safer manner.
32
27
33
-
### Do all the GraalVM polyglot features work?
28
+
### Do all the GraalVM polyglot features work with Python?
34
29
35
-
The team is continuously working to ensure all polyglot features of GraalVM work as a
36
-
Python user would expect. There are still many cases where expectations are
37
-
unclear or where multiple behaviours are imaginable. The team is actively looking at
38
-
use cases and are continuously evolving the Python implementation to provide the most
30
+
The team is continuously working to ensure all polyglot features of GraalVM work as a Python user would expect.
31
+
There are still many cases where expectations are unclear or where multiple behaviors are imaginable.
32
+
The team is actively looking at use cases and continuously evolving the Python implementation to provide the most
39
33
convenient and least surprising behaviour.
40
34
41
-
### What is the performance I can expect from GraalVM's Python implementation?
35
+
### What performance can I expect from GraalVM's Python runtime?
42
36
43
-
For pure Python code, performance after warm-up can be expected to be around 5-6
44
-
times faster than CPython 3.8 (or 6-7x faster than Jython). For native
45
-
extensions running as LLVM bitcode, CPython is currently slower -- you
46
-
can expect to see between 0.1x and 0.5x performance.
37
+
For the pure Python code, performance after warm-up can be expected to be around 5-6 times faster than CPython 3.8 (or 6-7x faster than Jython).
38
+
For native extensions running as LLVM bitcode, CPython is currently slower -- you can expect to see between 0.1x and 0.5x performance.
47
39
48
-
### I heard languages with JIT compilers have slow startup. Is that true for GraalVM's Python?
40
+
### I heard languages with JIT compilers have slow startup. Is that true for GraalVM's Python runtime?
49
41
50
-
It depends. When you use the [GraalVM Native Image](https://www.graalvm.org/reference-manual/native-image/) feature with Python or
51
-
use the `graalpython` launcher of GraalVM, its startup is competitive with
52
-
CPython. In any case, both with Native Image or when running on JVM you first
53
-
need to warm up to reach peak performance. This is a complicated story in
54
-
itself, but in general it can take a while (a minute or two) after you have
55
-
reached and are running your core workload.
42
+
It depends.
43
+
When you use [Native Image](https://www.graalvm.org/reference-manual/native-image/) with Python, or the `graalpython` launcher of GraalVM, startup is competitive with CPython.
44
+
In any case, both with Native Image or when running on the JVM, you first need to warm up to reach peak performance. This is a complicated story in itself, but, in general, it can take a while (a minute or two) after you have reached and are running your core workload.
56
45
57
-
### Can I share warmedup code between multiple Python contexts?
46
+
### Can I share warmed-up code between multiple Python contexts?
58
47
59
-
Yes, this works, and you will find that starting up multiple contexts in the
60
-
same engine and running the same or similar code in them will get increasingly
61
-
faster, because the compiled code is shared across contexts. However, the peak
62
-
performance in this setup is currently lower than in the single context case.
48
+
Yes, this works, and you will find that starting up multiple contexts in the same engine, and running the same or similar code in them will get increasingly faster, because the compiled code is shared across contexts.
49
+
However, the peak performance in this setup is currently lower than in the single context case.
This global should then work as expected; accessing attributes assumes it reads
54
-
from the `members` namespace; accessing items is supported both with strings and
55
-
numbers; calling methods on the result tries to do a straight invoke and falls
56
-
back to reading the member and trying to execute it.
57
-
58
-
59
-
## Examples
60
-
Here is an example of how to use JavaScript regular expression engine to
53
+
Here is an example of how to use the JavaScript regular expression engine to
61
54
match Python strings. Save this code to the `polyglot_example.py` file:
62
-
63
55
```python
64
56
import polyglot
65
57
@@ -77,26 +69,23 @@ if not md:
77
69
print("Here is what we found: '%s'"% md[1])
78
70
```
79
71
80
-
To run it, pass the `--jvm --polyglot`options to `graalpython`binary:
72
+
To run it, pass the `--jvm --polyglot`option to the `graalpython`launcher:
81
73
```shell
82
74
graalpython --jvm --polyglot polyglot_example.py
83
75
```
84
76
85
-
This example matches Python strings using the JavaScript regular expression object
86
-
and Python reads the captured group from the JavaScript result and prints: `Here
87
-
is what we found: 'This string was matched by Graal.js'`.
77
+
This program matches Python strings using the JavaScript regular expression object.
78
+
Python reads the captured group from the JavaScript result and prints:
79
+
*Here is what we found: 'This string was matched by Graal.js'*.
88
80
89
-
As a more complex example, we can read a file using R, process the data in
90
-
Python, and use R again to display the resulting data image, using both R and
91
-
Python libraries in conjunction. To run it, first install the
92
-
required R library:
81
+
As a more complex example, see how you can read a file using R, process the data in Python, and use R again to display the resulting data image, using both the R and Python libraries in conjunction.
82
+
To run this example, first install the required R library:
93
83
```shell
94
84
R -e 'install.packages("https://www.rforge.net/src/contrib/jpeg_0.1-8.tar.gz", repos=NULL)'
95
85
```
96
86
97
87
This example also uses [image_magix.py](http://graalvm.org/docs/examples/image_magix.py) and works
98
-
on a JPEG image input (you can try with [this image](https://www.graalvm.org/resources/img/python_demo_picture.jpg)). These files have to be in the same folder the script below is located in and executed from.
99
-
88
+
on a JPEG image input (you can try with [this image](https://www.graalvm.org/resources/img/python_demo_picture.jpg)). These files have to be in the same folder that the script below is located in and executed from.
100
89
```python
101
90
import polyglot
102
91
import sys
@@ -136,36 +125,36 @@ time.sleep(10)
136
125
137
126
## Java Interoperability
138
127
139
-
Finally, to interoperate with Java (only when running on the JVM), you can use
140
-
the `java` module:
128
+
Finally, to interoperate with Java (only when running on the JVM), you can use the `java` module:
141
129
```python
142
130
import java
143
131
BigInteger = java.type("java.math.BigInteger")
144
-
myBigInt = BigInteger.valueOf(42)
145
-
myBigInt.shiftLeft(128) # public Java methods can just be called
146
-
myBigInt["not"]() # Java method names that are keywords in
147
-
# Python can be accessed using "[]"
132
+
myBigInt = BigInteger(42)
133
+
myBigInt.shiftLeft(128)
134
+
# public Java methods can just be called
135
+
myBigInt["not"]()
136
+
# Java method names that are keywords in Python can be accessed using "[]"
148
137
byteArray = myBigInt.toByteArray()
149
-
print(list(byteArray)) # Java arrays can act like Python lists
138
+
# Java arrays can act like Python lists
139
+
print(list(byteArray))
150
140
```
151
141
152
142
For packages under the `java` package, you can also use the normal Python import
153
-
syntax. This syntax is limited to importing concrete classes, it doesn't work
154
-
on packages, unless in [Jython Compatibility](Jython.md) mode.
143
+
syntax:
155
144
```python
156
145
import java.util.ArrayList
157
146
from java.util import ArrayList
158
147
159
-
# these are the same class
160
148
java.util.ArrayList == ArrayList
161
149
162
150
al = ArrayList()
163
151
al.add(1)
164
152
al.add(12)
165
-
print(al) # prints [1, 12]
153
+
print(al)
154
+
# prints [1, 12]
166
155
```
167
156
168
-
In addition to the `type` builtin method, the `java` module, exposes the following
157
+
In addition to the `type` builtin method, the `java` module exposes the following
169
158
methods as well:
170
159
171
160
Builtin | Specification
@@ -179,12 +168,16 @@ Builtin | Specification
179
168
import java
180
169
ArrayList = java.type('java.util.ArrayList')
181
170
my_list = ArrayList()
182
-
print(java.is_symbol(ArrayList)) # prints True
183
-
print(java.is_symbol(my_list)) # prints False, my_list is not a Java host symbol
184
-
print(java.is_object(ArrayList)) # prints True, symbols are also host objects
185
-
print(java.is_function(my_list.add))# prints True, the add method of ArrayList
186
-
print(java.instanceof(my_list, ArrayList)) # prints True
171
+
print(java.is_symbol(ArrayList))
172
+
# prints True
173
+
print(java.is_symbol(my_list))
174
+
# prints False, my_list is not a Java host symbol
175
+
print(java.is_object(ArrayList))
176
+
# prints True, symbols are also host objects
177
+
print(java.is_function(my_list.add))
178
+
# prints True, the add method of ArrayList
179
+
print(java.instanceof(my_list, ArrayList))
180
+
# prints True
187
181
```
188
182
189
-
See the [Polyglot Programming](https://www.graalvm.org/reference-manual/polyglot-programming/) and the [Embed Languages](https://www.graalvm.org/reference-manual/embed-languages/) reference
190
-
for more information about interoperability with other programming languages.
183
+
See [Polyglot Programming](https://www.graalvm.org/docs/reference-manual/polyglot-programming/) and [Embed Languages](https://www.graalvm.org/reference-manual/embed-languages/) for more information about interoperability with other programming languages.
0 commit comments