Class PythonAsyncioStreams

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

@Internal public final class PythonAsyncioStreams extends Object
The Java half of Python-native streaming: a Reactive Streams Publisher consumed by a Python async for, and a Python async iterator exposed as a Publisher.

Both directions keep the reactive signals and the Python state on different sides of one rule: Python runs only on the asyncio loop that owns the stream. Reactive signals arriving on arbitrary threads are queued onto the Micronaut event loop, inside an execution frame of the stream's context, before any Python callback runs. Without a Micronaut loop (a plain asyncio loop in a test), the callbacks run on the signalling thread and the Python side hops onto its loop with call_soon_threadsafe. The Python state machines live in the micronaut_asyncio module (as_async_iterable and as_publisher).

Since:
5.2.3
  • Method Details

    • iterator

      public static PythonAsyncioStreams.PublisherIterator iterator(org.graalvm.polyglot.Value publisher, @Nullable PythonEventLoop eventLoop, org.graalvm.polyglot.Value callbacks, @Nullable PythonReactiveContext reactiveContext)
      Java entry point of as_async_iterable: a demand-driven subscriber over a publisher that reports its signals to Python callbacks.
      Parameters:
      publisher - The publisher, or any value convertible to one
      eventLoop - The Micronaut event loop of the consuming coroutine, or null
      callbacks - A Python object with on_next, on_error and on_complete
      reactiveContext - The reactive context of the consuming coroutine to subscribe within, or null
      Returns:
      The subscriber; Python calls PythonAsyncioStreams.PublisherIterator.request() and PythonAsyncioStreams.PublisherIterator.cancel()
    • publisher

      public static Publisher<Object> publisher(org.graalvm.polyglot.Value start, @Nullable PythonEventLoop eventLoop)
      Java entry point of as_publisher: a cold publisher whose every subscription starts a Python driver over a fresh async iterator.
      Parameters:
      start - A Python callable taking the PythonAsyncioStreams.GeneratorSubscription and returning the driver, a Python object with request(n) and cancel()
      eventLoop - The Micronaut event loop the driver runs on, or null for a plain asyncio loop
      Returns:
      The publisher