Last active
August 25, 2017 23:25
-
-
Save nuclearace/d67fba7c8f94c36d91e31e82863ec4ae to your computer and use it in GitHub Desktop.
DispatchIO bug linux
This file contains 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 CoreFoundation | |
import Dispatch | |
import Foundation | |
let pipe = Pipe() | |
let queue = DispatchQueue(label: "readQueue") | |
let source = DispatchIO(type: .stream, fileDescriptor: pipe.fileHandleForReading.fileDescriptor, | |
queue: queue, cleanupHandler: {code in | |
print("Source spent: \(code)") | |
}) | |
let writer = Process() | |
source.setLimit(lowWater: 101) | |
writer.launchPath = "/path/to/writer" | |
writer.standardOutput = pipe | |
writer.terminationHandler = {process in | |
print("writer done") | |
} | |
func readFromSource() { | |
source.read(offset: 0, length: 101, queue: queue) {done, data, code in | |
guard let data = data, data.count > 0 else { | |
print("No data, reader probably closed") | |
if done && code == 0 { | |
// EOF reached | |
print("Reader done") | |
exit(0) | |
} | |
print("Not done?") | |
return | |
} | |
print("Read \(data.count) bytes") | |
readFromSource() | |
} | |
} | |
readFromSource() | |
writer.launch() | |
CFRunLoopRun() |
This file contains 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
for i in 0..<100_000 { | |
print("This is a test") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment