Skip to content

StdinStream

Base classes

Name Children Inherits
ByteReceiveStream
anyio.abc
An interface for receiving bytes from a single peer.

⋔ Inheritance diagram

graph TD
  95970860614432["stdio.StdinStream"]
  95970857787856["abc.ByteReceiveStream"]
  95970858096192["abc.AsyncResource"]
  125067848591872["builtins.object"]
  95970857796432["_typedattr.TypedAttributeProvider"]
  95970857787856 --> 95970860614432
  95970858096192 --> 95970857787856
  125067848591872 --> 95970858096192
  95970857796432 --> 95970857787856
  125067848591872 --> 95970857796432

🛈 DocStrings

Bases: ByteReceiveStream

Wrapper for stdin that implements ByteReceiveStream interface.

Source code in src/aclientp/stdio.py
class StdinStream(ByteReceiveStream):
    """Wrapper for stdin that implements ByteReceiveStream interface."""

    async def receive(self, max_bytes: int = 65536) -> bytes:
        """Read bytes from stdin.

        Uses read1() which returns as soon as any data is available,
        rather than blocking until max_bytes are read.
        """
        loop = asyncio.get_running_loop()
        # read1() returns immediately when any data is available (up to max_bytes)
        # unlike read() which blocks until exactly max_bytes are read or EOF
        stdin_buffer = sys.stdin.buffer
        if not isinstance(stdin_buffer, io.BufferedIOBase):
            raise TypeError(f"Unsupported stdin buffer type: {type(stdin_buffer)}")
        data = await loop.run_in_executor(None, lambda: stdin_buffer.read1(max_bytes))
        if not data:
            raise anyio.EndOfStream
        return data

    async def aclose(self) -> None:
        """Close is a no-op for stdin."""

aclose async

aclose() -> None

Close is a no-op for stdin.

Source code in src/aclientp/stdio.py
async def aclose(self) -> None:
    """Close is a no-op for stdin."""

receive async

receive(max_bytes: int = 65536) -> bytes

Read bytes from stdin.

Uses read1() which returns as soon as any data is available, rather than blocking until max_bytes are read.

Source code in src/aclientp/stdio.py
async def receive(self, max_bytes: int = 65536) -> bytes:
    """Read bytes from stdin.

    Uses read1() which returns as soon as any data is available,
    rather than blocking until max_bytes are read.
    """
    loop = asyncio.get_running_loop()
    # read1() returns immediately when any data is available (up to max_bytes)
    # unlike read() which blocks until exactly max_bytes are read or EOF
    stdin_buffer = sys.stdin.buffer
    if not isinstance(stdin_buffer, io.BufferedIOBase):
        raise TypeError(f"Unsupported stdin buffer type: {type(stdin_buffer)}")
    data = await loop.run_in_executor(None, lambda: stdin_buffer.read1(max_bytes))
    if not data:
        raise anyio.EndOfStream
    return data

:fa-brands-github: Show source on GitHub