Class NettyPythonEventLoop
- All Implemented Interfaces:
PythonEventLoop
EventLoop.
This class is intentionally a narrow bridge between Python's asyncio transport protocol APIs and Micronaut's Netty event loop. It does not try to expose raw Netty channels to Python callers. The public nested transport/server/facade types are GraalPy host objects whose method names are part of the Python-facing surface, including snake_case names that mirror asyncio. Do not rename those methods to satisfy Java style checks unless the Python shims and tests are updated at the same time.
All callbacks into Python protocols are made from the Netty event loop thread. When this loop is entered from a different Java thread, work is rescheduled onto Netty before touching channel state or invoking Python callbacks.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classNetty-backed datagram transport exposed to Python as a host object.static final classNetty-backed asyncio server exposed to Python as a host object.static final classSocket-like object for Python's server.sockets API.static final classSocket-like facade for transport extras.static final classNetty-backed stream transport exposed to Python as a host object.static final classSSL facade for Python transport extras.static classWhat the Netty-backed stream and datagram transports share: the channel, the protocol, the provider that owns the channel, and the asyncio members that only depend on them. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionconnectAcceptedSocket(org.graalvm.polyglot.Value protocolFactory, Object socket, @Nullable Object ssl, @Nullable Double sslHandshakeTimeout, @Nullable Double sslShutdownTimeout) Wrap a Netty channel accepted elsewhere as an asyncio TCP transport.createConnection(org.graalvm.polyglot.Value protocolFactory, String host, int port, @Nullable String localHost, int localPort, @Nullable Object ssl, @Nullable String serverHostname, @Nullable Double sslHandshakeTimeout, @Nullable Double sslShutdownTimeout) Create a Netty-backed asyncio TCP connection.createConnection(org.graalvm.polyglot.Value protocolFactory, String host, int port, @Nullable String localHost, int localPort, @Nullable Object ssl, @Nullable String serverHostname, @Nullable Double sslHandshakeTimeout, @Nullable Double sslShutdownTimeout, String family) Connect a TCP client, resolving the host to an address of the requested family.createDatagramEndpoint(org.graalvm.polyglot.Value protocolFactory, @Nullable String localHost, int localPort, @Nullable String remoteHost, int remotePort, boolean allowBroadcast, boolean reusePort) Create a Netty-backed asyncio datagram endpoint.createDatagramEndpoint(org.graalvm.polyglot.Value protocolFactory, @Nullable String localHost, int localPort, @Nullable String remoteHost, int remotePort, boolean allowBroadcast, boolean reusePort, String family) Open a datagram endpoint on an address of the requested family.createServer(org.graalvm.polyglot.Value protocolFactory, @Nullable String host, int port, int backlog, boolean reuseAddress, boolean reusePort, boolean startServing, @Nullable Object ssl, @Nullable Double sslHandshakeTimeout, @Nullable Double sslShutdownTimeout) Create a Netty-backed asyncio TCP server.createServer(org.graalvm.polyglot.Value protocolFactory, @Nullable String host, int port, int backlog, boolean reuseAddress, boolean reusePort, boolean startServing, @Nullable Object ssl, @Nullable Double sslHandshakeTimeout, @Nullable Double sslShutdownTimeout, String family) Bind a TCP server on an address of the requested family.createUnixConnection(org.graalvm.polyglot.Value protocolFactory, String path, @Nullable Object ssl, @Nullable String serverHostname, @Nullable Double sslHandshakeTimeout, @Nullable Double sslShutdownTimeout) Create a Netty-backed asyncio Unix-domain socket connection.createUnixServer(org.graalvm.polyglot.Value protocolFactory, String path, int backlog, boolean startServing, @Nullable Object ssl, @Nullable Double sslHandshakeTimeout, @Nullable Double sslShutdownTimeout) Create a Netty-backed asyncio Unix-domain socket server.booleanio.netty.channel.EventLoopReturns the underlying Netty event loop.voidQueue a callback for serialized execution on the loop.voidexecuteCallback(org.graalvm.polyglot.Value callback) Queue a Python callback for execution on the loop.inthashCode()booleanUsed to decide whether Python work can run immediately or must be queued.Queue a callback after the requested delay using loop time.scheduleCallback(org.graalvm.polyglot.Value callback, long delay, TimeUnit unit) Queue a Python callback after the requested delay using loop time.doubletime()Returns the monotonic time base exposed to Python's asyncio APIs.
-
Constructor Details
-
NettyPythonEventLoop
public NettyPythonEventLoop(io.netty.channel.EventLoop eventLoop) - Parameters:
eventLoop- The Netty event loop.
-
-
Method Details
-
inEventLoop
public boolean inEventLoop()Description copied from interface:PythonEventLoopUsed to decide whether Python work can run immediately or must be queued.- Specified by:
inEventLoopin interfacePythonEventLoop- Returns:
- Whether the current thread is this event-loop thread.
-
execute
Description copied from interface:PythonEventLoopQueue a callback for serialized execution on the loop.- Specified by:
executein interfacePythonEventLoop- Parameters:
runnable- The callback.
-
schedule
Description copied from interface:PythonEventLoopQueue a callback after the requested delay using loop time.- Specified by:
schedulein interfacePythonEventLoop- Parameters:
runnable- The callback.delay- The delay.unit- The delay unit.- Returns:
- The scheduled future.
-
executeCallback
public void executeCallback(org.graalvm.polyglot.Value callback) Description copied from interface:PythonEventLoopQueue a Python callback for execution on the loop.A loop that owns a GraalPy context runs the callback inside an execution frame of the callback's context, so shutdown waits for callbacks scheduled with
call_soon. ThePropagatedContextof the callback is not captured here: it belongs to the asyncio task that owns the callback, which keeps it in itscontextvarsand restores it when the callback runs, so a task resumed by another task (a sharedasyncio.Event) or by a Java thread completing an awaited stage keeps its own context.- Specified by:
executeCallbackin interfacePythonEventLoop- Parameters:
callback- The Python callable.
-
scheduleCallback
public ScheduledFuture<?> scheduleCallback(org.graalvm.polyglot.Value callback, long delay, TimeUnit unit) Description copied from interface:PythonEventLoopQueue a Python callback after the requested delay using loop time.- Specified by:
scheduleCallbackin interfacePythonEventLoop- Parameters:
callback- The Python callable.delay- The delay.unit- The delay unit.- Returns:
- The scheduled future.
-
time
public double time()Description copied from interface:PythonEventLoopReturns the monotonic time base exposed to Python's asyncio APIs.- Specified by:
timein interfacePythonEventLoop- Returns:
- Event-loop time in seconds.
-
equals
-
hashCode
-
eventLoop
public io.netty.channel.EventLoop eventLoop()Returns the underlying Netty event loop.- Returns:
- The underlying Netty event loop.
-
createConnection
public CompletionStage<Object[]> createConnection(org.graalvm.polyglot.Value protocolFactory, String host, int port, @Nullable String localHost, int localPort, @Nullable Object ssl, @Nullable String serverHostname, @Nullable Double sslHandshakeTimeout, @Nullable Double sslShutdownTimeout) Create a Netty-backed asyncio TCP connection.- Parameters:
protocolFactory- The Python protocol factory.host- The remote host.port- The remote port.localHost- The local host, or null.localPort- The local port, or-1.ssl- TLS options.serverHostname- The TLS server hostname.sslHandshakeTimeout- The TLS handshake timeout in seconds.sslShutdownTimeout- The TLS shutdown timeout in seconds.- Returns:
- A stage completing with transport and protocol.
-
createConnection
public CompletionStage<Object[]> createConnection(org.graalvm.polyglot.Value protocolFactory, String host, int port, @Nullable String localHost, int localPort, @Nullable Object ssl, @Nullable String serverHostname, @Nullable Double sslHandshakeTimeout, @Nullable Double sslShutdownTimeout, String family) Connect a TCP client, resolving the host to an address of the requested family.- Parameters:
protocolFactory- The asyncio protocol factory.host- The remote host.port- The remote port.localHost- The local host to bind, or null.localPort- The local port to bind, or -1.ssl- TLS options, or null.serverHostname- The TLS server name, or null.sslHandshakeTimeout- The TLS handshake timeout in seconds.sslShutdownTimeout- The TLS shutdown timeout in seconds.family-inet,inet6or empty for any address family- Returns:
- A stage completing with transport and protocol.
-
createServer
public CompletionStage<NettyPythonEventLoop.NettyServer> createServer(org.graalvm.polyglot.Value protocolFactory, @Nullable String host, int port, int backlog, boolean reuseAddress, boolean reusePort, boolean startServing, @Nullable Object ssl, @Nullable Double sslHandshakeTimeout, @Nullable Double sslShutdownTimeout) Create a Netty-backed asyncio TCP server.- Parameters:
protocolFactory- The Python protocol factory.host- The local host.port- The local port.backlog- The listen backlog.reuseAddress- Whether address reuse is enabled.reusePort- Whether port reuse is requested.startServing- Whether to start accepting connections immediately.ssl- TLS options.sslHandshakeTimeout- The TLS handshake timeout in seconds.sslShutdownTimeout- The TLS shutdown timeout in seconds.- Returns:
- A stage completing with the server.
-
createServer
public CompletionStage<NettyPythonEventLoop.NettyServer> createServer(org.graalvm.polyglot.Value protocolFactory, @Nullable String host, int port, int backlog, boolean reuseAddress, boolean reusePort, boolean startServing, @Nullable Object ssl, @Nullable Double sslHandshakeTimeout, @Nullable Double sslShutdownTimeout, String family) Bind a TCP server on an address of the requested family.- Parameters:
protocolFactory- The asyncio protocol factory.host- The host to bind, or null for every address.port- The port to bind.backlog- The accept backlog.reuseAddress- Whether to setSO_REUSEADDR.reusePort- Whether to setSO_REUSEPORT.startServing- Whether to accept connections right away.ssl- TLS options, or null.sslHandshakeTimeout- The TLS handshake timeout in seconds.sslShutdownTimeout- The TLS shutdown timeout in seconds.family-inet,inet6or empty for any address family- Returns:
- A stage completing with the server.
-
connectAcceptedSocket
public CompletionStage<Object[]> connectAcceptedSocket(org.graalvm.polyglot.Value protocolFactory, Object socket, @Nullable Object ssl, @Nullable Double sslHandshakeTimeout, @Nullable Double sslShutdownTimeout) Wrap a Netty channel accepted elsewhere as an asyncio TCP transport.- Parameters:
protocolFactory- The Python protocol factory.socket- The accepted Netty channel or transport exposing one.ssl- TLS options.sslHandshakeTimeout- The TLS handshake timeout in seconds.sslShutdownTimeout- The TLS shutdown timeout in seconds.- Returns:
- A stage completing with transport and protocol.
-
createUnixConnection
public CompletionStage<Object[]> createUnixConnection(org.graalvm.polyglot.Value protocolFactory, String path, @Nullable Object ssl, @Nullable String serverHostname, @Nullable Double sslHandshakeTimeout, @Nullable Double sslShutdownTimeout) Create a Netty-backed asyncio Unix-domain socket connection.- Parameters:
protocolFactory- The Python protocol factory.path- The Unix-domain socket path.ssl- TLS options.serverHostname- The TLS server hostname.sslHandshakeTimeout- The TLS handshake timeout in seconds.sslShutdownTimeout- The TLS shutdown timeout in seconds.- Returns:
- A stage completing with transport and protocol.
-
createUnixServer
public CompletionStage<NettyPythonEventLoop.NettyServer> createUnixServer(org.graalvm.polyglot.Value protocolFactory, String path, int backlog, boolean startServing, @Nullable Object ssl, @Nullable Double sslHandshakeTimeout, @Nullable Double sslShutdownTimeout) Create a Netty-backed asyncio Unix-domain socket server.- Parameters:
protocolFactory- The Python protocol factory.path- The Unix-domain socket path.backlog- The listen backlog.startServing- Whether to start accepting connections immediately.ssl- TLS options.sslHandshakeTimeout- The TLS handshake timeout in seconds.sslShutdownTimeout- The TLS shutdown timeout in seconds.- Returns:
- A stage completing with the server.
-
createDatagramEndpoint
public CompletionStage<Object[]> createDatagramEndpoint(org.graalvm.polyglot.Value protocolFactory, @Nullable String localHost, int localPort, @Nullable String remoteHost, int remotePort, boolean allowBroadcast, boolean reusePort) Create a Netty-backed asyncio datagram endpoint.- Parameters:
protocolFactory- The Python protocol factory.localHost- The local host, or null.localPort- The local port, or-1.remoteHost- The remote host, or null.remotePort- The remote port, or-1.allowBroadcast- Whether broadcast is enabled.reusePort- Whether port reuse is requested.- Returns:
- A stage completing with transport and protocol.
-
createDatagramEndpoint
public CompletionStage<Object[]> createDatagramEndpoint(org.graalvm.polyglot.Value protocolFactory, @Nullable String localHost, int localPort, @Nullable String remoteHost, int remotePort, boolean allowBroadcast, boolean reusePort, String family) Open a datagram endpoint on an address of the requested family.- Parameters:
protocolFactory- The asyncio datagram protocol factory.localHost- The local host, or null.localPort- The local port, or -1.remoteHost- The remote host to connect to, or null.remotePort- The remote port, or -1.allowBroadcast- Whether to setSO_BROADCAST.reusePort- Whether to setSO_REUSEPORT.family-inet,inet6or empty for any address family- Returns:
- A stage completing with transport and protocol.
-