Os.openPipe
func Os.openPipe(): Result[Os.Pipe, Error]Os.openPipe returns a pipe, two handles the program closes: what is written to write is read from read. A read waits for data. The reading end is at end of file once every writing end is closed, the copies given to children and the program's own. A write waits while the pipe is full, so the program reads it on another goroutine, or gives it to a child, before writing much. A write once every copy of read is closed is Err with the message "broken pipe", and the program goes on.
Details
- A
ctxon a read ofpipe.readends the wait. After Cmd.start givespipe.writeto a child, close the program's own copy, orpipe.readnever reaches end of file. - A child that writes into a pipe nobody reads blocks once the pipe is full. Read the pipe before Cmd.wait, or on another goroutine. The program's own write waits the same way.
- A write once every copy of
pipe.readis closed isErrwith the messagebroken pipe, and the program goes on. Only a broken pipe on standard output or standard error ends the program.
Basic examples
What is written to pipe.write is read from pipe.read. Closing the write end makes the next read see the end:
let pipe = try Os.openPipe()
defer { pipe.read.close() }
try pipe.write.writeLine("hello")
try pipe.write.close()
println(try pipe.read.readLine())
println(try pipe.read.readLine())hello
nullRead a child's output line by line, as it writes it:
let out = try Os.openPipe()
defer { out.read.close() }
let child = try Cmd.start(Cmd.new("seq", ["3"]), stdout: out.write)
try out.write.close()
while true {
let line = try out.read.readLine() ?? break
println("got ${line}")
}
println(try child.wait())got 1
got 2
got 3
0Scope
Write a child's standard input a piece at a time. Here tr writes to the program's standard output, the default, and stops once the write end is closed:
let input = try Os.openPipe()
defer { input.read.close() }
let child = try Cmd.start(Cmd.new("tr", ["a-z", "A-Z"]), stdin: input.read)
for word in ["ant", "bee", "cat"] {
try input.write.writeLine(word)
}
try input.write.close()
println(try child.wait())ANT
BEE
CAT
0Give the same write end as stdout and stderr to merge both outputs into one pipe, as 2>&1 does:
let pipe = try Os.openPipe()
defer { pipe.read.close() }
let script = "echo out; echo err >&2; echo out again"
let child = try Cmd.start(Cmd.new("sh", ["-c", script]), stdout: pipe.write, stderr: pipe.write)
try pipe.write.close()
print(try pipe.read.readAll())
println(try child.wait())out
err
out again
0Two pipes keep the outputs apart. One goroutine reads standard error while the program reads standard output, so neither pipe fills up:
let out = try Os.openPipe()
let err = try Os.openPipe()
defer { out.read.close() }
defer { err.read.close() }
let script = "echo built main; echo 'warning: unused x' >&2; echo built test"
let child = try Cmd.start(Cmd.new("sh", ["-c", script]), stdout: out.write, stderr: err.write)
try out.write.close()
try err.write.close()
let warnings: Channel[string] = Channel.new(1)
go {
warnings.send(err.read.readAll().unwrap())
}
print(try out.read.readAll())
print("stderr: ${warnings.receive() ?? ""}")
println(try child.wait())built main
built test
stderr: warning: unused x
0A pipeline from Cmd.pipe writes into a pipe as one command does. Its last stage writes to pipe.write:
let pipe = try Os.openPipe()
defer { pipe.read.close() }
let words = Cmd.pipe([Cmd.new("printf", ["b\\na\\nb\\nc\\n"]), Cmd.new("sort", ["-u"])])
let child = try Cmd.start(words, stdout: pipe.write)
try pipe.write.close()
print(try pipe.read.readAll())
println(try child.wait())a
b
c
0A pipe joins two goroutines of the program too. The writer closes its end when it is done, which ends the reader's loop:
let pipe = try Os.openPipe()
defer { pipe.read.close() }
go {
defer { pipe.write.close() }
for n in Array.range(1, 4) {
pipe.write.writeLine("tick ${n}").unwrap()
}
}
while true {
let line = try pipe.read.readLine() ?? break
println(line)
}tick 1
tick 2
tick 3Applications
Run a command and tag each line of its output, both streams merged, as cmd 2>&1 | sed 's/^/[build] /' does. The write end is closed before try looks at the result of Cmd.start, so it is closed when the start fails too:
func runTagged(tag: string, c: Cmd): Result[int, Error] {
let pipe = try Os.openPipe()
defer { pipe.read.close() }
let started = Cmd.start(c, stdout: pipe.write, stderr: pipe.write)
try pipe.write.close()
let child = try started
while true {
let line = try pipe.read.readLine() ?? break
println("[${tag}] ${line}")
}
child.wait()
}
let script = "echo compiling; echo 'warning: slow' >&2; echo done"
let status = try runTagged("build", Cmd.new("sh", ["-c", script]))
println("exit ${status}")[build] compiling
[build] warning: slow
[build] done
exit 0Properties and relations
A pipe passes bytes through unchanged, and after the write end is closed every read is at the end:
let pipe = try Os.openPipe()
defer { pipe.read.close() }
let data = Bytes.fromArray([0, 1, 254, 255])
try pipe.write.writeChunk(data)
try pipe.write.close()
assert(try pipe.read.readExactly(4) == data)
assert(try pipe.read.readChunk(10) == null)
assert(try pipe.read.readLine() == null)Each call makes a new pipe. What is written to one is read only from that one:
let a = try Os.openPipe()
let b = try Os.openPipe()
defer { a.read.close() }
defer { b.read.close() }
defer { a.write.close() }
defer { b.write.close() }
try a.write.writeLine("to a")
try b.write.writeLine("to b")
assert(try b.read.readLine() == "to b")
assert(try a.read.readLine() == "to a")Possible issues
The read end is not at its end while the program's own copy of the write end is open, even after the child has exited. Without the close, File.readAll waits forever; here a context stops it:
let pipe = try Os.openPipe()
defer { pipe.read.close() }
let child = try Cmd.start(Cmd.new("seq", ["3"]), stdout: pipe.write)
// The program's copy of the write end is still open.
println(pipe.read.readAll(ctx: Context.new(100ms)))
try pipe.write.close()
println(try child.wait())Err{ error: Error{ message: "timeout" } }
0Waiting for a child before reading its pipe can wait forever: once the pipe is full the child blocks on its next write and never exits. Here a context stops the wait, and reading the pipe lets the child finish:
let pipe = try Os.openPipe()
defer { pipe.read.close() }
let child = try Cmd.start(Cmd.new("seq", ["100000"]), stdout: pipe.write)
try pipe.write.close()
println(child.wait(ctx: Context.new(100ms)))
let lines = try pipe.read.readAll() | String.lines()
println(lines.length())
println(try child.wait())Err{ error: Error{ message: "timeout" } }
100000
0A write after the read end is closed has nowhere to go. It is Err, and the program goes on:
let pipe = try Os.openPipe()
defer { pipe.write.close() }
try pipe.read.close()
println(pipe.write.writeLine("x"))
println("still running")Err{ error: Error{ message: "broken pipe" } }
still runningSee also
Cmd.start · Cmd.wait · Cmd.pipe · File.readLine · File.readAll · File.close · Os.Pipe · Os.stdout
