When compiling the following code in macOS it runs and gives output.
JavaScript
x
import Foundation
let runloop = RunLoop.current
let process = Process()
process.launchPath = "/bin/echo"
process.arguments = ["hello world"]
let output = Pipe()
process.standardOutput = output
output.fileHandleForReading.readabilityHandler = { fileHandle in
let data = fileHandle.availableData
print("received data: (data.count)")
print(String(data: data, encoding: .utf8) ?? "")
}
print("starting")
process.launch()
while runloop.run(mode: .default, before: Date(timeIntervalSinceNow: 2)) { }
output:
JavaScript
received data: 12
hello worldn
But when I compile and run this in a Ubuntu Swift docker image (Swift version 5.0.1), the readabilityHandler
closure doesn’t get called at all.
Advertisement
Answer
This appears to be fixed in the swift:latest
Docker image (5.1.4 at the time of writing). It looks like readabilityHandler
was implemented in Linux and merged in the run-up to Swift 5.1.