Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Reading stdin input

Read from stdin by installing a handler with env.stdio.stdin_install. The handler receives data as it arrives. In the common text case, the data is decoded to str. After the last input, the handler gets None.

actor main(env):
    def interact(input):
        if input is not None:
            print("Got some input:", input)

    env.stdio.stdin_install(interact)

stdin can be a terminal, a pipe, a socket or a file, as in ./prog < input.txt. Installing a handler again replaces the previous one, and reading goes on where it was.

The None comes once stdin has ended: at the end of a file or a pipe, when Ctrl-D is pressed in a terminal, or when reading stdin fails. A program that reads all of its input can finish there.

actor main(env):
    var lines = 0

    def count(input):
        if input is not None:
            lines += input.count("\n")
        else:
            print("Lines:", lines)
            env.exit(0)

    env.stdio.stdin_install(count)

Installing a handler after stdin has ended starts reading again. A file or a pipe that has ended gives None right away, and a terminal gives what is typed after Ctrl-D. For text input, bytes left over from a character that never ended go to on_error before the None.

You can make the text decoding explicit by providing on_stdin, encoding, and on_error.

When encoding is not set, Acton tries to discover the encoding from LANG. If nothing useful is found, it falls back to UTF-8.

actor main(env):
    def interact(input):
        if input is not None:
            print("Got some input:", input)

    def on_stdin_error(err, data):
        print("Some error with decoding the input data:", err)
        print("Raw bytes data:", data)

    env.stdio.stdin_install(on_stdin=interact, encoding="utf-8",
        on_error=on_stdin_error)

If the data is binary, or if you want to delay decoding, install a bytes handler instead.

actor main(env):
    def interact(bytes_input):
        # Decode only when this code is ready to decide how.
        if bytes_input is not None:
            print("Got some input:", bytes_input.decode())

    env.stdio.stdin_install(on_stdin_bytes=interact)

The important distinction is between a decoded text stream and a raw byte stream. If the input protocol is truly textual, the string path is usually right. If framing, binary payloads, or uncertain encodings matter, keep the data as bytes until your own code decides how to decode it.

Common patterns

Use the text callback for ordinary command line input, especially when the input is line-oriented or clearly textual.

Use the bytes callback when the input may contain binary data, partial fragments of multibyte characters, or a protocol with its own framing rules.

Treat decode errors as part of the interface. If the program expects text, decide what to do when the bytes do not decode cleanly instead of letting that decision hide inside a helper.