Class PythonContextRuntime
java.lang.Object
io.micronaut.context.python.PythonContextRuntime
Runtime coordination point for generated Python bridge classes.
This type owns access to the primary GraalPy context, resolves classes and scripts from the
primary context or a PythonPool, mirrors host members into event-loop contexts, executes
shared Python helper functions, and tracks active executions so contexts and engines can be
closed safely.
- Since:
- 5.2.0
- Author:
- Micronaut Team
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordPre-split Python class identity used by generated bridge code. -
Method Summary
Modifier and TypeMethodDescriptionstatic org.graalvm.polyglot.ValueasyncInstance(org.graalvm.polyglot.Value fallback, PythonContextRuntime.PythonClassReference classReference) Resolve a Python instance for the current asyncio event loop when one is active.static org.graalvm.polyglot.ValueenumValue(PythonContextRuntime.PythonClassReference classReference, String name) Resolve a Python enum constant by its Java enum name.static org.graalvm.polyglot.ValuefindClass(PythonContextRuntime.PythonClassReference classReference) Find a Python class by pre-split class reference.static org.graalvm.polyglot.ValuefindPooledClass(PythonContextRuntime.PythonClassReference classReference) Obtain a pooled Python class instance (per-context cached).static org.graalvm.polyglot.ValuefindPooledClass(PythonContextRuntime.PythonClassReference classReference, org.graalvm.polyglot.Context context) Obtain a pooled Python class instance from a specific context.static org.graalvm.polyglot.ValuefindPooledScript(String packageName, String scriptName) Obtain a pooled Python script/module object.static org.graalvm.polyglot.ValuefindPooledScript(String packageName, String scriptName, org.graalvm.polyglot.Context context) Obtain a pooled Python script/module object from a specific context.static org.graalvm.polyglot.ValuefindScript(String packageName, String scriptName) Find a Python script/module Value.static org.graalvm.polyglot.Contextstatic @Nullable ClassLoaderReturns the application class loader associated with the current GraalPy context.static voidinjectPooledScript(String packageName, String scriptName, String attribute, Object value) Inject an attribute into all pooled script contexts.static voidinjectPooledScriptAsync(String packageName, String scriptName, String attribute, Object value) Inject an async-adapted attribute into all pooled script contexts.static org.graalvm.polyglot.ValueinvokePooled(PythonContextRuntime.PythonClassReference classReference, String methodName, Object... args) Invoke a method on a pooled class instance.static org.graalvm.polyglot.ValueinvokePooledScript(String packageName, String scriptName, String methodName, Object... args) Invoke a method on a pooled script instance.static org.graalvm.polyglot.ValueinvokeStaticMethod(PythonContextRuntime.PythonClassReference classReference, String methodName, Object... args) Invoke a static method on the given Python class.static booleanCheck if the context has been initialized.static booleanReturns true if the context should be reused.static org.graalvm.polyglot.ValuenewFrozenDataclassInstance(PythonContextRuntime.PythonClassReference classReference, Map<String, Object> props) Create a new frozen dataclass instance and set properties without invoking __init__.static org.graalvm.polyglot.ValuenewInstance(PythonContextRuntime.PythonClassReference classReference, Object... args) Create a new instance for the given class reference and args.static org.graalvm.polyglot.ValuenewInstance(PythonContextRuntime.PythonClassReference classReference, Map<String, Object> props) Create a new instance and set properties via member assignment when no constructor exists.static org.graalvm.polyglot.ValuenewInstanceWithDefaultedTrailingNulls(PythonContextRuntime.PythonClassReference classReference, int requiredArgCount, Object... args) Create a new instance, omitting trailing null arguments that correspond to Python constructor defaults.static org.graalvm.polyglot.ValuenewIntroduction(PythonContextRuntime.PythonClassReference classReference, Object... args) Create an instance that is abstract and fill out the abstract methods with stubs to be later populated.static org.graalvm.polyglot.ValuenewIntroductionWithDefaultedTrailingNulls(PythonContextRuntime.PythonClassReference classReference, int requiredArgCount, Object... args) Create a new abstract introduction instance, omitting trailing null arguments that correspond to Python constructor defaults.static org.graalvm.polyglot.ValuenewUninitializedInstance(PythonContextRuntime.PythonClassReference classReference) Create a Python instance without invoking__init__.static voidrememberAsyncMember(org.graalvm.polyglot.Value source, String name, @Nullable Object value) Remember a host-side member assigned to a Python object so async event-loop contexts can mirror it.static voidReset the context to null.static voidsetContext(org.graalvm.polyglot.Context context) Set the GraalPy context.static voidsetContext(org.graalvm.polyglot.Context context, @Nullable ClassLoader classLoader) Set the GraalPy context and the application class loader that should be active when generated bridge classes enter Python from arbitrary runtime threads.static voidsetReuseContext(boolean reuse) If context reuse is set to true, then the context will never be cleared.static <T> TwithPooled(PythonContextRuntime.PythonClassReference classReference, Function<org.graalvm.polyglot.Value, T> fn) Execute a function with a borrowed pooled class instance.static <T> TwithPooledScript(String packageName, String scriptName, Function<org.graalvm.polyglot.Value, T> fn) Execute a function with a borrowed pooled script/module object.static PooledValuewithPooledValue(String expression) Create a wrapper for a Python value that is evaluated and cached in each pooled context.static <T> TwithPooledValue(String expression, Function<org.graalvm.polyglot.Value, T> fn) Execute a callback with a Python value evaluated and cached in a borrowed pooled context.
-
Method Details
-
asyncInstance
public static org.graalvm.polyglot.Value asyncInstance(org.graalvm.polyglot.Value fallback, PythonContextRuntime.PythonClassReference classReference) Resolve a Python instance for the current asyncio event loop when one is active.- Parameters:
fallback- The startup-context instanceclassReference- The Python class reference- Returns:
- An event-loop-local instance, or the fallback when no event-loop context is active
- Since:
- 5.2.0
-
rememberAsyncMember
public static void rememberAsyncMember(org.graalvm.polyglot.Value source, String name, @Nullable Object value) Remember a host-side member assigned to a Python object so async event-loop contexts can mirror it.- Parameters:
source- The startup-context Python object.name- The member name.value- The host value.
-
findPooledClass
public static org.graalvm.polyglot.Value findPooledClass(PythonContextRuntime.PythonClassReference classReference) Obtain a pooled Python class instance (per-context cached).- Parameters:
classReference- The Python class reference- Returns:
- The pooled class instance (Value) from some context
- Since:
- 5.2.0
-
findPooledClass
public static org.graalvm.polyglot.Value findPooledClass(PythonContextRuntime.PythonClassReference classReference, org.graalvm.polyglot.Context context) Obtain a pooled Python class instance from a specific context.- Parameters:
classReference- The Python class referencecontext- The context- Returns:
- The pooled class instance (Value)
- Since:
- 5.2.0
-
withPooled
public static <T> T withPooled(PythonContextRuntime.PythonClassReference classReference, Function<org.graalvm.polyglot.Value, T> fn) Execute a function with a borrowed pooled class instance.- Type Parameters:
T- Result type returned by the function- Parameters:
classReference- The Python class referencefn- Function receiving the pooled Value- Returns:
- Result returned from the function
- Since:
- 5.2.0
-
findPooledScript
-
findPooledScript
public static org.graalvm.polyglot.Value findPooledScript(String packageName, String scriptName, org.graalvm.polyglot.Context context) Obtain a pooled Python script/module object from a specific context.- Parameters:
packageName- The Python packagescriptName- The script/module namecontext- The context- Returns:
- A pooled script Value
-
withPooledScript
public static <T> T withPooledScript(String packageName, String scriptName, Function<org.graalvm.polyglot.Value, T> fn) Execute a function with a borrowed pooled script/module object.- Type Parameters:
T- Result type returned by the function- Parameters:
packageName- The packagescriptName- The script namefn- Function receiving the script Value- Returns:
- Result returned from the function
-
withPooledValue
Create a wrapper for a Python value that is evaluated and cached in each pooled context.- Parameters:
expression- The Python expression or statements to evaluate- Returns:
- A pooled value wrapper
- Since:
- 5.2.0
-
withPooledValue
Execute a callback with a Python value evaluated and cached in a borrowed pooled context.- Type Parameters:
T- Result type returned by the function- Parameters:
expression- The Python expression or statements to evaluatefn- Function receiving the pooled value- Returns:
- Result returned from the function
- Since:
- 5.2.0
-
injectPooledScript
-
injectPooledScriptAsync
public static void injectPooledScriptAsync(String packageName, String scriptName, String attribute, Object value) Inject an async-adapted attribute into all pooled script contexts.- Parameters:
packageName- The packagescriptName- The script nameattribute- The attribute namevalue- The value to inject
-
invokePooled
public static org.graalvm.polyglot.Value invokePooled(PythonContextRuntime.PythonClassReference classReference, String methodName, Object... args) Invoke a method on a pooled class instance.- Parameters:
classReference- The Python class referencemethodName- The method nameargs- Arguments- Returns:
- The polyglot result
- Since:
- 5.2.0
-
invokePooledScript
public static org.graalvm.polyglot.Value invokePooledScript(String packageName, String scriptName, String methodName, Object... args) Invoke a method on a pooled script instance.- Parameters:
packageName- The packagescriptName- The script namemethodName- The method nameargs- Arguments- Returns:
- The polyglot result
-
getContext
public static org.graalvm.polyglot.Context getContext() -
newIntroduction
public static org.graalvm.polyglot.Value newIntroduction(PythonContextRuntime.PythonClassReference classReference, Object... args) Create an instance that is abstract and fill out the abstract methods with stubs to be later populated.- Parameters:
classReference- The Python class referenceargs- The args- Returns:
- The new instance
- Since:
- 5.2.0
-
newIntroductionWithDefaultedTrailingNulls
public static org.graalvm.polyglot.Value newIntroductionWithDefaultedTrailingNulls(PythonContextRuntime.PythonClassReference classReference, int requiredArgCount, Object... args) Create a new abstract introduction instance, omitting trailing null arguments that correspond to Python constructor defaults.- Parameters:
classReference- The Python class referencerequiredArgCount- The number of non-defaulted positional constructor argumentsargs- The arguments- Returns:
- The new instance
- Since:
- 5.2.0
-
newInstance
public static org.graalvm.polyglot.Value newInstance(PythonContextRuntime.PythonClassReference classReference, Object... args) Create a new instance for the given class reference and args.- Parameters:
classReference- The Python class referenceargs- The args- Returns:
- The new instance
- Since:
- 5.2.0
-
enumValue
public static org.graalvm.polyglot.Value enumValue(PythonContextRuntime.PythonClassReference classReference, String name) Resolve a Python enum constant by its Java enum name.- Parameters:
classReference- The Python class referencename- The enum constant name- Returns:
- The Python enum constant
- Since:
- 5.2.0
-
newInstanceWithDefaultedTrailingNulls
public static org.graalvm.polyglot.Value newInstanceWithDefaultedTrailingNulls(PythonContextRuntime.PythonClassReference classReference, int requiredArgCount, Object... args) Create a new instance, omitting trailing null arguments that correspond to Python constructor defaults.- Parameters:
classReference- The Python class referencerequiredArgCount- The number of non-defaulted positional constructor argumentsargs- The arguments- Returns:
- The new instance
- Since:
- 5.2.0
-
newUninitializedInstance
public static org.graalvm.polyglot.Value newUninitializedInstance(PythonContextRuntime.PythonClassReference classReference) Create a Python instance without invoking__init__.- Parameters:
classReference- The Python class reference- Returns:
- The new uninitialized instance
- Since:
- 5.2.0
-
newInstance
public static org.graalvm.polyglot.Value newInstance(PythonContextRuntime.PythonClassReference classReference, Map<String, Object> props) Create a new instance and set properties via member assignment when no constructor exists.- Parameters:
classReference- The Python class referenceprops- Map of property names to values- Returns:
- The new instance with members populated
- Since:
- 5.2.0
-
newFrozenDataclassInstance
public static org.graalvm.polyglot.Value newFrozenDataclassInstance(PythonContextRuntime.PythonClassReference classReference, Map<String, Object> props) Create a new frozen dataclass instance and set properties without invoking __init__.- Parameters:
classReference- The Python class referenceprops- Map of property names to values- Returns:
- The new frozen dataclass instance with members populated
- Since:
- 5.2.0
-
findClass
public static org.graalvm.polyglot.Value findClass(PythonContextRuntime.PythonClassReference classReference) Find a Python class by pre-split class reference.- Parameters:
classReference- The Python class reference- Returns:
- The class Value
- Since:
- 5.2.0
-
findScript
-
invokeStaticMethod
public static org.graalvm.polyglot.Value invokeStaticMethod(PythonContextRuntime.PythonClassReference classReference, String methodName, Object... args) Invoke a static method on the given Python class.- Parameters:
classReference- The Python class referencemethodName- The method nameargs- The method arguments- Returns:
- The method result
- Since:
- 5.2.0
-
setContext
public static void setContext(org.graalvm.polyglot.Context context) Set the GraalPy context. This method is called by GraalPyContextFactory during application startup.- Parameters:
context- The GraalPy context to set
-
setContext
public static void setContext(org.graalvm.polyglot.Context context, @Nullable ClassLoader classLoader) Set the GraalPy context and the application class loader that should be active when generated bridge classes enter Python from arbitrary runtime threads.- Parameters:
context- The GraalPy context to setclassLoader- The application class loader used to build the context
-
getContextClassLoader
Returns the application class loader associated with the current GraalPy context.- Returns:
- The application class loader
-
isInitialized
public static boolean isInitialized()Check if the context has been initialized.- Returns:
- true if the context is available, false otherwise
-
resetContext
public static void resetContext()Reset the context to null. This method is called during application shutdown to ensure proper cleanup and prevent memory leaks. -
setReuseContext
public static void setReuseContext(boolean reuse) If context reuse is set to true, then the context will never be cleared.- Parameters:
reuse- tells if the context should be reused
-
isReuseContext
public static boolean isReuseContext()Returns true if the context should be reused.- Returns:
- the reuse flag
-