Os.Pipe
type Os.Pipe = { read: File.Handle, write: File.Handle }Os.Pipe is a pair of connected handles, made by Os.openPipe: what is written to write is read from read.
Details
Os.Pipeis a record of two File.Handle values. TheFilefunctions readreadand writewrite; the pipe itself has no functions.- The program closes both ends.
readis at end of file once every copy ofwriteis closed, including the copies given to children byCmd.start. - A read of
readwaits until data comes or every copy ofwriteis closed; passctx:to give up, aspipe.read.readLine(ctx: c). - Copying the record, by assignment, a spread, or a call, copies the handles, not the pipe: every copy names the same two ends, so closing one copy's end closes them all.
Cmd.pipejoins commands with pipes of its own. Use anOs.Pipefor what it does not join: a child's output read as it comes, its input written a piece at a time, or the two outputs kept apart.
Basic examples
Os.openPipe returns an Os.Pipe. Write to its write field and read from its read field:
let pipe = try Os.openPipe()
defer { pipe.read.close() }
try pipe.write.writeLine("hello")
try pipe.write.close()
println(try pipe.read.readLine())helloDestructure the two ends into names of their own:
let { read, write } = try Os.openPipe()
defer { read.close() }
try write.writeLine("through the pipe")
try write.close()
print(try read.readAll())through the pipeA pipe renders as a record of two handles:
let pipe = try Os.openPipe()
defer { pipe.read.close() }
defer { pipe.write.close() }
println(pipe)Os.Pipe{ read: File.Handle, write: File.Handle }Scope
Rename the ends as you destructure them, to say which way the data flows:
let { read: fromChild, write: childOut } = try Os.openPipe()
defer { fromChild.close() }
let child = try Cmd.start(Cmd.new("echo", ["ready"]), stdout: childOut)
try childOut.close()
println(try fromChild.readLine())
println(try child.wait())ready
0Take an Os.Pipe as a parameter. This function writes lines to the pipe and closes its write end, on an error too, so the caller reads to the end:
func send(pipe: Os.Pipe, lines: Array[string]): Result[void, Error] {
defer { pipe.write.close() }
for line in lines {
try pipe.write.writeLine(line)
}
pipe.write.close()
}
let pipe = try Os.openPipe()
defer { pipe.read.close() }
try send(pipe, ["one", "two"])
print(try pipe.read.readAll())one
twoAccept a pipe or a plain handle, and tell them apart with is:
func sink(to: File.Handle | Os.Pipe): File.Handle {
if to is Os.Pipe { to.write } else { to }
}
let pipe = try Os.openPipe()
defer { pipe.read.close() }
defer { pipe.write.close() }
println(sink(pipe) == pipe.write)
println(sink(Os.stdout()) == Os.stdout())true
trueApplications
Read a child's output line by line as it comes. readLine returns null once the child has exited and the program has closed its copy of the write end:
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()
if line == null {
break
}
println("got ${line}")
}
println(try child.wait())got 1
got 2
got 3
0Write a child's input a piece at a time. The child reads to the end of its input, so the program closes the write end when it is done:
let input = try Os.openPipe()
let child = try Cmd.start(Cmd.new("sort"), stdin: input.read)
try input.read.close()
for word in ["pear", "apple", "fig"] {
try input.write.writeLine(word)
}
try input.write.close()
println(try child.wait())apple
fig
pear
0Start several commands, each writing into a pipe of its own, and read each output whole. The write end is closed before Cmd.start's result is looked at, so it is closed when the start fails too:
type Job = { name: string, out: Os.Pipe, process: Cmd.Process }
func startJob(name: string, c: Cmd): Result[Job, Error] {
let out = try Os.openPipe()
let started = Cmd.start(c, stdout: out.write)
try out.write.close()
if started is Err {
_ = out.read.close()
return Result.err(started.error)
}
Result.ok(Job{ name, out, process: started.value })
}
let jobs = [
try startJob("greet", Cmd.new("printf", [`hello\nworld\n`])),
try startJob("count", Cmd.new("seq", ["3"])),
]
for job in jobs {
defer { job.out.read.close() }
let lines = try job.out.read.readAll() | String.lines()
let status = try job.process.wait()
println("${job.name}: ${lines.length()} lines, exit ${status}")
}greet: 2 lines, exit 0
count: 3 lines, exit 0Properties and relations
The two ends of a pipe are different handles, and a new call makes a new pipe. A destructured end and a spread copy hold the same handles as the original:
let a = try Os.openPipe()
let b = try Os.openPipe()
defer { a.read.close() }
defer { a.write.close() }
defer { b.read.close() }
defer { b.write.close() }
assert(a.read != a.write)
assert(a != b)
let { read } = a
assert(read == a.read)
assert(Os.Pipe{ ...a } == a)Two pipes between two children do what Cmd.pipe does with one call. Each child gets its own copy of the ends it is given, so the program closes its copies of link once both have started. Cmd.read drops the output's final newline, so the comparison puts it back:
let words = Cmd.new("printf", [`pear\napple\npear\n`])
let sort = Cmd.new("sort", ["-u"])
let link = try Os.openPipe()
let out = try Os.openPipe()
defer { out.read.close() }
let first = try Cmd.start(words, stdout: link.write)
let second = try Cmd.start(sort, stdin: link.read, stdout: out.write)
try link.write.close()
try link.read.close()
try out.write.close()
let byHand = try out.read.readAll()
assert(try first.wait() == 0)
assert(try second.wait() == 0)
assert(byHand == "${try Cmd.read(Cmd.pipe([words, sort]))}\n")
print(byHand)apple
pearPossible issues
A copy of the record is not a new pipe. Closing the write end through the copy closes the original's too:
let pipe = try Os.openPipe()
defer { pipe.read.close() }
let copy = Os.Pipe{ ...pipe }
try copy.write.close()
println(pipe.write.writeLine("late"))Err{ error: Error{ message: "the file is closed" } }A literal that takes its ends from two pipes is not connected: what is written to its write goes into the other pipe. Here a context stops the read that would otherwise wait forever:
let a = try Os.openPipe()
let b = try Os.openPipe()
defer { a.read.close() }
defer { a.write.close() }
defer { b.read.close() }
defer { b.write.close() }
let mixed = Os.Pipe{ read: a.read, write: b.write }
try mixed.write.writeLine("hello")
println(mixed.read.readLine(ctx: Context.new(100ms)))
println(try b.read.readLine())Err{ error: Error{ message: "timeout" } }
helloOs.Pipe has no close. Close each end:
let pipe = try Os.openPipe()
pipe.close() // error: Os.Pipe has no field or function closeSee also
Os.openPipe · File.Handle · File.close · Cmd.start · Cmd.pipe · Cmd.Pipeline
