Skip to content

Commit 318bc7a

Browse files
committed
[GR-71196] Tech writer run through
PullRequest: graalpython/4088
2 parents fe9d49e + e32e7d1 commit 318bc7a

File tree

2 files changed

+33
-57
lines changed

2 files changed

+33
-57
lines changed

docs/site/_config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
baseurl: "/python"
22
url: "https://graalvm.org"
33
github: "oracle/graalpython"
4-
language_version: 25.0.0
4+
language_version: 25.0.1
55
name: GraalPy
66

77
permalink: pretty
Lines changed: 32 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,68 @@
11
# Native Executables with Python
22

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.
44

55
## Quickstart
66

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).
88

99
To build the application, run:
10+
1011
```bash
1112
mvn -Pnative package
1213
```
13-
The command packages the project and creates a native executable.
1414

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.
2116

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.
2321

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
5723

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.
5827

5928
### Removing Pre-initialized Python Heap
6029

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:
6637

6738
```bash
68-
-Dimage-build-time.PreinitializeContexts=
39+
-Dpolyglot.image-build-time.PreinitializeContexts=
6940
```
7041

7142
### Disabling Runtime Compilation of Python Code
7243

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+
7452
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+
7554
This can be achieved by passing the following options:
7655

7756
```bash
78-
-Dtruffle.TruffleRuntime=com.oracle.truffle.api.impl.DefaultTruffleRuntime \
57+
-Dtruffle.TruffleRuntime=com.oracle.truffle.api.impl.DefaultTruffleRuntime
7958
-Dpolyglot.engine.WarnInterpreterOnly=false
8059
```
8160

8261
### Summary
8362

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.
8665

8766
## Shipping Python Packages
8867

8968
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

Comments
 (0)