Advanced Usage
Advanced Python4J features — GIL management, context managers, garbage collection, and subprocess mode
GIL Management
Why the GIL Matters
PythonGIL.lock()
try (PythonGIL gil = PythonGIL.lock()) {
PythonExecutioner.exec("x = 42");
}Multi-threaded Usage
Runnable task = () -> {
try (PythonGIL gil = PythonGIL.lock()) {
PythonExecutioner.exec("import time; time.sleep(0.1)");
}
};
Thread t1 = new Thread(task);
Thread t2 = new Thread(task);
t1.start();
t2.start();
t1.join();
t2.join();assertThreadSafe()
Embedded Python (Disabling Automatic GIL Release)
PythonContextManager
The Problem Context Isolation Solves
Creating and Switching Contexts
PythonContextManager.Context (AutoCloseable)
Resetting and Deleting Contexts
PythonGC — Python Object Garbage Collection
Why Manual GC Exists
PythonGC.watch()
PythonGC.keep()
PythonGC.pause() and resume()
Subprocess Mode — PythonProcess
Running a Command and Collecting Output
Running a Script File
Package Management
Error Handling
System Properties Reference
Property
Default
Description
Last updated
Was this helpful?