Class PythonAsyncioRuntime
java.lang.Object
io.micronaut.context.python.PythonAsyncioRuntime
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classCompletes futures with Java exceptions from Python exception data.static final classRunsasyncio.run_in_executor(None, ...)callbacks on Micronaut's blocking executor.static final classCompletableFuture variant that can propagate Java cancellation to a Python task. -
Method Summary
Modifier and TypeMethodDescriptionstatic org.graalvm.polyglot.ValueasyncioHelper(org.graalvm.polyglot.Context context, String name) Resolve a Java entry point of themicronaut_asynciomodule, cached per context.static org.graalvm.polyglot.ValueawaitJava(org.graalvm.polyglot.Value value) Python entry point ofawaiton a Java object: the asyncio future of aCompletionStage, or of the first item of a publisher (subscribed within the reactive context of the awaiting coroutine), so aCompletableFutureorMonoa Java call returned is awaitable like the value of an injected client.static org.graalvm.polyglot.ValueawaitPublisher(org.graalvm.polyglot.Value publisher, @Nullable PythonReactiveContext reactiveContext) Python entry point of the publisher awaitable: the asyncio future of the publisher's first item.static voidcompleteOnLoop(CompletionStage<?> stage, @Nullable PythonEventLoop eventLoop, org.graalvm.polyglot.Value callback) Complete a Python callback with a Java stage's outcome on the event loop, inside an execution frame of the callback's context; the callback is skipped once the context is closing.static @Nullable org.graalvm.polyglot.ValueThe Python asyncio loop of the current Micronaut event loop, installed on demand.static PublishergeneratorToPublisher(@Nullable org.graalvm.polyglot.Value value) Expose the async iterator (an async generator object, typically) a bridge method returned as a Reactive StreamsPublisher.static voidreportLoopError(String text) Log an event-loop callback failure reported by the Python loop's default exception handler.static voidsetEnabled(boolean enabled) Set whether Python asyncio bridge execution is enabled.static org.graalvm.polyglot.ValuetoAwaitable(org.graalvm.polyglot.Context context, CompletionStage<?> stage) Wrap a JavaCompletionStageas an asyncio future for Pythonawait.static CompletionStagetoCompletionStage(org.graalvm.polyglot.Value value) Convert a Python coroutine or awaitable value into a JavaCompletionStage.toPublisher(org.graalvm.polyglot.Value value) Convert a Python coroutine into a publisher that starts the coroutine when it is first subscribed, in the reactive context of that subscriber: the publishers the coroutine awaits are subscribed with the Reactor context (a reactive transaction status, for instance) and the propagated context of the subscriber.
-
Method Details
-
toCompletionStage
Convert a Python coroutine or awaitable value into a JavaCompletionStage.- Parameters:
value- The Python result value.- Returns:
- A stage that completes when the Python awaitable completes.
-
toPublisher
Convert a Python coroutine into a publisher that starts the coroutine when it is first subscribed, in the reactive context of that subscriber: the publishers the coroutine awaits are subscribed with the Reactor context (a reactive transaction status, for instance) and the propagated context of the subscriber. ACompletionStageis eager; a bridgedasync defdeclared to return a publisher uses this deferred form instead.Consequences of the deferred form: the coroutine never runs when nobody subscribes (Python then warns that the coroutine was never awaited), the event loop is the one of the subscribing thread, so a
subscribeOndecides which loop runs the coroutine, and the result of the first subscription is shared with later subscribers, a cancelled subscription leaving the coroutine running for them.- Parameters:
value- The Python coroutine or awaitable value.- Returns:
- A publisher of the coroutine's result, empty when the coroutine returns
None.
-
generatorToPublisher
Expose the async iterator (an async generator object, typically) a bridge method returned as a Reactive StreamsPublisher. The iterator is advanced on the current Micronaut event loop as the subscriber requests elements; without one, and without a running Python loop, the call fails rather than driving the generator synchronously.- Parameters:
value- The Python async iterator- Returns:
- A cold publisher that can be subscribed to once
-
awaitPublisher
@Internal public static org.graalvm.polyglot.Value awaitPublisher(org.graalvm.polyglot.Value publisher, @Nullable PythonReactiveContext reactiveContext) Python entry point of the publisher awaitable: the asyncio future of the publisher's first item.- Parameters:
publisher- The publisherreactiveContext- The reactive context to subscribe within, ornullfor none- Returns:
- The future
-
awaitJava
Python entry point ofawaiton a Java object: the asyncio future of aCompletionStage, or of the first item of a publisher (subscribed within the reactive context of the awaiting coroutine), so aCompletableFutureorMonoa Java call returned is awaitable like the value of an injected client.- Parameters:
value- The Java object- Returns:
- The future
-
currentAsyncioLoop
The Python asyncio loop of the current Micronaut event loop, installed on demand. Called by themicronaut_asynciomodule when a stream is created outside a running coroutine.- Returns:
- The loop, or
nullwhen the calling thread has no admitted event loop
-
toAwaitable
public static org.graalvm.polyglot.Value toAwaitable(org.graalvm.polyglot.Context context, CompletionStage<?> stage) Wrap a JavaCompletionStageas an asyncio future for Pythonawait.- Parameters:
context- The Python context.stage- The Java completion stage.- Returns:
- An asyncio future.
-
setEnabled
public static void setEnabled(boolean enabled) Set whether Python asyncio bridge execution is enabled.- Parameters:
enabled- Whether async bridge execution is enabled.
-
reportLoopError
-
asyncioHelper
-
completeOnLoop
@Internal public static void completeOnLoop(CompletionStage<?> stage, @Nullable PythonEventLoop eventLoop, org.graalvm.polyglot.Value callback) Complete a Python callback with a Java stage's outcome on the event loop, inside an execution frame of the callback's context; the callback is skipped once the context is closing. This is how the asyncio module observes Java stages: a Python callable is never registered with aCompletionStagedirectly, which would run it on the completing thread outside any frame.- Parameters:
stage- The Java stageeventLoop- The loop to complete on, or null to complete on the completing threadcallback- A Python callable taking the value and the throwable
-