Class PythonAsyncioRuntime

java.lang.Object
io.micronaut.context.python.PythonAsyncioRuntime

@Internal public final class PythonAsyncioRuntime extends Object
Runtime helpers for Python coroutine bridge methods.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
    Completes futures with Java exceptions from Python exception data.
    static final class 
    Runs asyncio.run_in_executor(None, ...) callbacks on Micronaut's blocking executor.
    static final class 
    CompletableFuture variant that can propagate Java cancellation to a Python task.
  • Method Summary

    Modifier and Type
    Method
    Description
    static org.graalvm.polyglot.Value
    asyncioHelper(org.graalvm.polyglot.Context context, String name)
    Resolve a Java entry point of the micronaut_asyncio module, cached per context.
    static org.graalvm.polyglot.Value
    awaitJava(org.graalvm.polyglot.Value value)
    Python entry point of await on a Java object: the asyncio future of a CompletionStage, or of the first item of a publisher (subscribed within the reactive context of the awaiting coroutine), so a CompletableFuture or Mono a Java call returned is awaitable like the value of an injected client.
    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.
    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.
    static @Nullable org.graalvm.polyglot.Value
    The Python asyncio loop of the current Micronaut event loop, installed on demand.
    static Publisher
    generatorToPublisher(@Nullable org.graalvm.polyglot.Value value)
    Expose the async iterator (an async generator object, typically) a bridge method returned as a Reactive Streams Publisher.
    static void
    Log an event-loop callback failure reported by the Python loop's default exception handler.
    static void
    setEnabled(boolean enabled)
    Set whether Python asyncio bridge execution is enabled.
    static org.graalvm.polyglot.Value
    toAwaitable(org.graalvm.polyglot.Context context, CompletionStage<?> stage)
    Wrap a Java CompletionStage as an asyncio future for Python await.
    toCompletionStage(org.graalvm.polyglot.Value value)
    Convert a Python coroutine or awaitable value into a Java CompletionStage.
    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.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • toCompletionStage

      public static CompletionStage toCompletionStage(org.graalvm.polyglot.Value value)
      Convert a Python coroutine or awaitable value into a Java CompletionStage.
      Parameters:
      value - The Python result value.
      Returns:
      A stage that completes when the Python awaitable completes.
    • toPublisher

      public static Publisher<Object> 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. A CompletionStage is eager; a bridged async def declared 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 subscribeOn decides 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

      public static Publisher generatorToPublisher(@Nullable org.graalvm.polyglot.Value value)
      Expose the async iterator (an async generator object, typically) a bridge method returned as a Reactive Streams Publisher. 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 publisher
      reactiveContext - The reactive context to subscribe within, or null for none
      Returns:
      The future
    • awaitJava

      @Internal public static org.graalvm.polyglot.Value awaitJava(org.graalvm.polyglot.Value value)
      Python entry point of await on a Java object: the asyncio future of a CompletionStage, or of the first item of a publisher (subscribed within the reactive context of the awaiting coroutine), so a CompletableFuture or Mono a Java call returned is awaitable like the value of an injected client.
      Parameters:
      value - The Java object
      Returns:
      The future
    • currentAsyncioLoop

      @Internal public static @Nullable org.graalvm.polyglot.Value currentAsyncioLoop()
      The Python asyncio loop of the current Micronaut event loop, installed on demand. Called by the micronaut_asyncio module when a stream is created outside a running coroutine.
      Returns:
      The loop, or null when 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 Java CompletionStage as an asyncio future for Python await.
      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

      @Internal public static void reportLoopError(String text)
      Log an event-loop callback failure reported by the Python loop's default exception handler.
      Parameters:
      text - The formatted report, traceback included
    • asyncioHelper

      @Internal public static org.graalvm.polyglot.Value asyncioHelper(org.graalvm.polyglot.Context context, String name)
      Resolve a Java entry point of the micronaut_asyncio module, cached per context.
      Parameters:
      context - The context
      name - The module-level function name
      Returns:
      The function
    • 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 a CompletionStage directly, which would run it on the completing thread outside any frame.
      Parameters:
      stage - The Java stage
      eventLoop - The loop to complete on, or null to complete on the completing thread
      callback - A Python callable taking the value and the throwable