Class PythonAsyncioStreams
java.lang.Object
io.micronaut.context.python.PythonAsyncioStreams
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classThe subscription handed to a Java subscriber of a Python async iterator, and the facade the Python driver signals through.static final classSubscribes to a publisher one item at a time on behalf of a Python async iterator. -
Method Summary
Modifier and TypeMethodDescriptioniterator(org.graalvm.polyglot.Value publisher, @Nullable PythonEventLoop eventLoop, org.graalvm.polyglot.Value callbacks, @Nullable PythonReactiveContext reactiveContext) Java entry point ofas_async_iterable: a demand-driven subscriber over a publisher that reports its signals to Python callbacks.publisher(org.graalvm.polyglot.Value start, @Nullable PythonEventLoop eventLoop) Java entry point ofas_publisher: a cold publisher whose every subscription starts a Python driver over a fresh async iterator.
-
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 ofas_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 oneeventLoop- The Micronaut event loop of the consuming coroutine, ornullcallbacks- A Python object withon_next,on_errorandon_completereactiveContext- The reactive context of the consuming coroutine to subscribe within, ornull- Returns:
- The subscriber; Python calls
PythonAsyncioStreams.PublisherIterator.request()andPythonAsyncioStreams.PublisherIterator.cancel()
-
publisher
public static Publisher<Object> publisher(org.graalvm.polyglot.Value start, @Nullable PythonEventLoop eventLoop) Java entry point ofas_publisher: a cold publisher whose every subscription starts a Python driver over a fresh async iterator.- Parameters:
start- A Python callable taking thePythonAsyncioStreams.GeneratorSubscriptionand returning the driver, a Python object withrequest(n)andcancel()eventLoop- The Micronaut event loop the driver runs on, ornullfor a plain asyncio loop- Returns:
- The publisher
-