代码之家  ›  专栏  ›  技术社区  ›  Marcel

进程的fileHandleForReading可读性Handler闭包在Linux上未被调用

  •  0
  • Marcel  · 技术社区  · 7 年前

    在macOS中编译以下代码时,它会运行并给出输出。

    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)) { }
    

    输出:

    received data: 12
    hello world\n
    

    但是,当我在Ubuntu Swift docker镜像(Swift 5.0.1版)中编译并运行它时 readabilityHandler 根本就没有人要求结束。

    0 回复  |  直到 7 年前
        1
  •  1
  •   John M.    6 年前

    这似乎已在 swift:latest Docker图像(撰写本文时为5.1.4)。看起来像 readabilityHandler 在Linux和 merged 在Swift 5.1的准备阶段。