Created
November 29, 2022 20:42
-
-
Save jbadger3/be66e2e64cc3084fb433252495a7df7a to your computer and use it in GitHub Desktop.
Demo using async-await to read from a FileHandle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
let outputPipe = Pipe() | |
let process = Process() | |
process.executableURL = URL(fileURLWithPath:"/bin/bash") | |
process.arguments = ["-c", "ls"] | |
process.standardOutput = outputPipe | |
try? process.run() | |
for try await line in outputPipe.fileHandleForReading.bytes.lines { | |
print(line) | |
} | |
let standardInputFH = process.standardInput as! FileHandle | |
let standardErrorFH = process.standardError as! FileHandle | |
print("PID: \(process.processIdentifier)") | |
print("stdin FD: \(standardInputFH.fileDescriptor)") | |
print("stdout FD: \(outputPipe.fileHandleForReading.fileDescriptor)") | |
print("stderr FD: \(standardErrorFH.fileDescriptor)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment