How does a Python process get a batch per line instead of a record per line?
Available — v0.0.1 (experimental)pythonbatch
A Python script in batch mode
The manifest declares batch: {maxItems: 500, maxWaitMs: 100}; the script reads one JSON array per line and prints one JSON array per line. Standard library only.
Available in v0.0.1: the files, README and tests are in examples/batch-python of the openflange-go repository. Commands run from the repository root.
Needs: python3 on PATH.
A pipe is a data-processing step with declared inputs and outputs.
What goes in, what comes out
Input (stdin)
{"id":1,"email":"Alice@Example.COM"}
{"id":2,"email":"x"}Command
printf '%s\n' '{"id":1,"email":"Alice@Example.COM"}' '{"id":2,"email":"x"}' \
| openflange run ./examples/batch-python --grant subprocessOutput (stdout)
{"id":1,"email":"Alice@Example.COM","domain":"example.com"}
{"id":2,"email":"x","domain":null}Files and further commands
examples/batch-python/OPENFLANGE.md — the batch declaration (excerpt)
scope: record
cardinality: one-to-one
deterministic: true
batch:
maxItems: 500
maxWaitMs: 100examples/batch-python/domain.py
#!/usr/bin/env python3
# SPDX-License-Identifier: Apache-2.0
"""email-domain-batch: adds `domain` to every record.
Batch mode, delivery `lines`: every line on stdin is a JSON array of records
(a batch); every line on stdout is the JSON array of the results, same length,
same order. Works without OpenFlange too:
printf '%s\n' '[{"email":"Alice@Example.COM"},{"email":"nope"}]' | python3 domain.py
[{"email": "Alice@Example.COM", "domain": "example.com"}, {"email": "nope", "domain": null}]
"""
import json
import sys
def domain(record):
email = record.get("email") if isinstance(record, dict) else None
if isinstance(email, str) and email.count("@") == 1 and not email.endswith("@"):
return email.rsplit("@", 1)[1].lower()
return None
for line in sys.stdin:
batch = json.loads(line)
out = [{**r, "domain": domain(r)} if isinstance(r, dict) else {"value": r, "domain": None} for r in batch]
sys.stdout.write(json.dumps(out, ensure_ascii=False, separators=(",", ":")))
sys.stdout.write("\n")
sys.stdout.flush() # one answer per batch: the runner waits for itRun its portable tests
openflange test ./examples/batch-pythonWhat to notice
- Every stdin line is an array of 1 to
maxItemsrecords. Every stdout line is an array too, exactly one per input line ([]if a batch has no output). That line is how the runner knows the batch is done, so flush it. - The pipe declares
cardinality: one-to-one, so each answer must have exactly as many elements as its batch. maxWaitMs: 100means no record is held back for more than 100 ms. While the script works on a batch the runner keeps collecting the next one.- The script is still an ordinary Unix filter: its docstring shows how to feed it an array line without OpenFlange.
OPENFLANGE_CONFIGcarries"batch": {"maxItems": 500, "maxWaitMs": 100, "delivery": "lines"}. The framing is defined in the CLI specification, section 5.1; the idea is explained in Batch mode.- Re-run for this page with the v0.0.1 CLI: the output matched the README.