Skip to content

JSON workers should delimit work requests with newlines #14218

@wolfd

Description

@wolfd

Description of the problem / feature request:

JSON workers are difficult to write because work requests are not newline delimited. This issue was brought up in #13599, and the discussion generally indicated that this is a usability bug.

The proposal is to add a newline between requests according to ndjson.

Feature requests: what underlying problem are you trying to solve with this feature?

Right now an implementation of a JSON worker work request loop using python looks like this:

# This is a pretty cumbersome way of getting work requests.
# It waits for something to be available on stdin, and then tries parsing
# whatever text is available as JSON. It keeps accumulating more text until a
# full work request is available.
def wait_for_stdin_json():
    stdin = open("/dev/stdin", "rb")
    os.set_blocking(stdin.fileno(), False)
    stream_bytes = b""
    while True:
        _, _, _ = select.select([stdin.fileno()], [], [])
        maybe_bytes = stdin.read()
        if maybe_bytes is None:
            continue
        stream_bytes += maybe_bytes
        try:
            work_request = json.loads(stream_bytes)
        except json.decoder.JSONDecodeError:
            # stream couldn't decode, wait until next select
            sys.stderr.write(f"failed to decode: {stream_bytes.decode('utf-8')}\n")
            sys.stderr.flush()
            continue

        yield work_request
        stream_bytes = b""  # reset

With newline-delimited JSON it would look like this:

# Simplified json loop with newline-delimited json.
def wait_for_stdin_json():
    while True:
        yield json.loads(sys.stdin.readline())

Bugs: what's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

Current version

Proposed version

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions