|
1 | 1 | # Native Executables with Python |
2 | 2 |
|
3 | | -GraalPy supports GraalVM Native Image to generate native binaries of Java applications that use GraalPy. |
| 3 | +GraalPy supports GraalVM Native Image to generate native binaries of Java applications that embed Python code. |
4 | 4 |
|
5 | 5 | ## Quickstart |
6 | 6 |
|
7 | | -If you started with the [Maven archetype](README.md), the generated _pom.xml_ makes it easy to generate a native executable using the [Maven plugin for Native Image building](https://graalvm.github.io/native-build-tools/latest/maven-plugin.html). |
| 7 | +If you started with the [Maven archetype](README.md), the generated _pom.xml_ file already includes the necessary configuration for creating a native executable using the [Maven plugin for Native Image building](https://graalvm.github.io/native-build-tools/latest/maven-plugin.html). |
8 | 8 |
|
9 | 9 | To build the application, run: |
| 10 | + |
10 | 11 | ```bash |
11 | 12 | mvn -Pnative package |
12 | 13 | ``` |
13 | | -The command packages the project and creates a native executable. |
14 | 14 |
|
15 | | -Take a look at the generated files _pom.xml_ and _Main.java_. |
16 | | -They are documented to explain how Python resources are included in the resulting binary. |
17 | | -The generated project should be viewed as a starting point. |
18 | | -It includes the entire Python standard library, so the Python code can invoke all of the standard library code. |
19 | | -The resources can be manually pruned to reduce the included Python libraries to the necessary amount, reducing both the size of the package and the time to start up. |
20 | | -This Java example demonstrates some useful default options for the Python context, but other settings may be desirable to further control what the Python code is allowed to do. |
| 15 | +This command packages the project and creates a native executable. |
21 | 16 |
|
22 | | -## Reducing Binary Size |
| 17 | +The generated _pom.xml_ and _Main.java_ files explain how Python resources are included in the resulting binary. |
| 18 | +The generated project serves as a starting point and includes the entire Python standard library by default, allowing your Python code to use any standard library modules. |
| 19 | +You can manually remove unused Python libraries to reduce both the executable size and startup time. |
| 20 | +The created example demonstrates useful default options for the Python context, but you can adjust these settings to control what your Python code can access. |
23 | 21 |
|
24 | | -Python is a large language. |
25 | | -"Batteries included" has long been a core tenet of CPython. |
26 | | -As a compatible replacement, GraalPy includes most of those "batteries" as well. |
27 | | -This can result in significant increases in binary size when including GraalPy in a Java application. |
28 | | - |
29 | | -Only you as the developer know your specific embedding scenario. |
30 | | -The defaults may include much more than needed for any specific application. |
31 | | -An embedded Python-in-Java application usually has more limited use cases for the Python interpreter than the full GraalPy distribution, and often you can know ahead of time whether certain features are needed. |
32 | | -Some features (for example, cryptographic algorithms or socket access) may even be undesirable to include in some cases. |
33 | | -Thus, when embedding GraalPy in a Java application, the binary size can be reduced and security improved by excluding components of the Python language. |
34 | | - |
35 | | -### Excluding Python Components |
36 | | - |
37 | | -GraalPy defines a few system properties that can be passed on the `native-image` command line to exclude aspects of the language. |
38 | | -The options can, when taken together, reduce the size of the executable by around 20%. |
39 | | -These are: |
40 | | - |
41 | | -* `python.WithoutSSL=true` - This option removes the `ssl` module. |
42 | | - If no secure network access or certificate checking is required, this removes Java's SSL classes and the BouncyCastle library. |
43 | | -* `python.WithoutDigest=true` - This option removes the `_md5`, `_sha1`, `_sha256`, `_sha512`, `_sha3`, and `_hashlib` modules. |
44 | | - This removes the direct usages of `java.security.MessageDigest` and `javax.crypto.Mac` from GraalPy. |
45 | | -* `python.WithoutPlatformAccess=true` - This removes the `signal` and `subprocess` modules, removes accesses to process properties such as the Unix UID and GID, or setting the Java default time zone. |
46 | | - This has no significant impact on binary size, but if these are unwanted capabilities that are dynamically disabled with context options anyway, they can also be removed ahead of time. |
47 | | -* `python.WithoutCompressionLibraries=true` - This removes the `zlib`, `lzma`, `bzip2`, and `zipimporter` modules and related classes. |
48 | | - These modules have both native and pure Java implementations (the former for performance, the latter for better sandboxing); however, if they are not needed, they can be removed entirely. |
49 | | -* `python.WithoutNativePosix=true` - The default `os` module backend is a pure Java implementation when GraalPy is embedded rather than run via its launcher. |
50 | | - The native POSIX backend of GraalPy is recommended only for 100% compatibility with CPython's POSIX interfaces, and if not used, can be removed from the build with this option. |
51 | | -* `python.WithoutJavaInet=true` - The Java implementation of Python's `socket` module is based on Java's networking classes. |
52 | | - If network access is denied for an embedding scenario, this option can reduce the binary size further. |
53 | | -* `python.AutomaticAsyncActions=false` - Signal handling, Python weak reference callbacks, and cleaning up native resources is usually achieved automatically by spawning GraalPy daemon threads that submit safepoint actions to the Python main thread. |
54 | | - This uses an `ExecutorService` with a thread pool. |
55 | | - If you want to disallow such extra threads or avoid pulling in `ExecutorService` and related classes, then set this property to `false` and retrieve the `PollPythonAsyncActions` object from the context's polyglot bindings. |
56 | | - This object is executable and can be used to trigger Python asynchronous actions at the locations you desire. |
| 22 | +## Reducing Binary Size |
57 | 23 |
|
| 24 | +Python is a feature-rich language with an extensive standard library. |
| 25 | +This can result in large native executables when embedding GraalPy in Java applications. |
| 26 | +You can significantly reduce the size by excluding components your application doesn't need by considering what your Python code actually uses. |
58 | 27 |
|
59 | 28 | ### Removing Pre-initialized Python Heap |
60 | 29 |
|
61 | | -Another useful option to reduce the size of the native executable is to omit a pre-initialized Python context from the executable. |
62 | | -By default, a default Python context is already pre-initialized and ready for immediate execution. |
63 | | -This can lead to slightly improved startup, at the cost of including a few thousand Python objects in the binary. |
64 | | -In embedded applications that use a custom polyglot engine to allow context sharing, the pre-initialized context cannot be used at all, and including those objects is wasted. |
65 | | -The pre-initialized heap can be omitted by passing the following to the `native-image` command: |
| 30 | +By default, GraalPy includes a pre-initialized Python context in the executable for faster startup. |
| 31 | +Disabling this reduces the binary size by about 15MiB. |
| 32 | +You should remove this if: |
| 33 | +- You are creating more than one context |
| 34 | +- Binary size is more important than a slight startup delay |
| 35 | + |
| 36 | +To remove the pre-initialized heap, add this flag to your build configuration: |
66 | 37 |
|
67 | 38 | ```bash |
68 | | --Dimage-build-time.PreinitializeContexts= |
| 39 | +-Dpolyglot.image-build-time.PreinitializeContexts= |
69 | 40 | ``` |
70 | 41 |
|
71 | 42 | ### Disabling Runtime Compilation of Python Code |
72 | 43 |
|
73 | | -If binary size is significantly more important than execution speed (which may be the case if all Python scripts are expected to be short running, perform a lot of I/O, or scripts are rarely executed more than once), it may make sense to disable JIT compilation entirely. |
| 44 | +If binary size is significantly more important than execution speed, you can disable JIT compilation entirely. |
| 45 | +This will reduce binary size by around 40%. |
| 46 | +You should use this if: |
| 47 | + |
| 48 | +- Your Python scripts are very short-running |
| 49 | +- Performance is not critical |
| 50 | +- Your scripts spend most time on I/O operations |
| 51 | + |
74 | 52 | Be aware that this may significantly impact your Python performance, so be sure to test the runtime behavior of your actual use cases when choosing to use this option. |
| 53 | + |
75 | 54 | This can be achieved by passing the following options: |
76 | 55 |
|
77 | 56 | ```bash |
78 | | --Dtruffle.TruffleRuntime=com.oracle.truffle.api.impl.DefaultTruffleRuntime \ |
| 57 | +-Dtruffle.TruffleRuntime=com.oracle.truffle.api.impl.DefaultTruffleRuntime |
79 | 58 | -Dpolyglot.engine.WarnInterpreterOnly=false |
80 | 59 | ``` |
81 | 60 |
|
82 | 61 | ### Summary |
83 | 62 |
|
84 | | -Combining all of these approaches can halve the size of the GraalPy binary. |
85 | | -Every embedded application is different and the code pulled in by the rest of the Java code also matters, so combinations of these options should be tried to determine which effect they have in a specific instance. |
| 63 | +Combining these approaches can reduce the binary size by 50% or more. |
| 64 | +Since every application is different, experiment with different combinations to find what works best for your specific use case. |
86 | 65 |
|
87 | 66 | ## Shipping Python Packages |
88 | 67 |
|
89 | 68 | Our Maven archetype by default is set up to include all needed Python files in the native binary itself, so the image is self-contained. |
90 | | - |
91 | | -In custom embeddings, the Python standard library is copied next to the native image. |
92 | | -When moving the native image, the standard library folder needs to be kept next to it. |
0 commit comments