【问题标题】:OCaml flush strange behaviour (exiting all threads)OCaml 刷新奇怪的行为(退出所有线程)
【发布时间】:2021-03-24 01:26:46
【问题描述】:

我尝试使用 2 个命令实现 CLI:

  • run/my/path/file.sock 处生成一个 unix 套接字并产生输出
  • watch 连接到套接字并将输出转发到自己的输出

当我杀死watch 命令时,我无法理解为什么run 命令也被杀死。它们不会引发任何异常或 sys 信号。我注意到问题来自flush 函数。一旦被调用,它就像exit 0。怎么会?

来源:https://github.com/soywod/comodoro

【问题讨论】:

    标签: sockets ocaml channel


    【解决方案1】:

    服务器必须监听客户端断开连接并从 conn 列表中删除连接。 这样的事情可能会奏效:

    let client_thread conn =
      let in_ch = in_channel_of_descr conn in
      while true do
        try input_line in_ch |> ignore
        with End_of_file ->
         (* Remove conn from !conn and close the conn *)
      done
    in
    
    let add_conn () =
      (* ... *)
      Thread.create client_thread conn |> ignore;
      (* ... *)
    in
    

    【讨论】:

    • 确实,我不想听in频道,因为我不需要,但实际上必须知道客户端何时断开连接。现在像魅力一样工作,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-06
    • 1970-01-01
    • 2015-01-27
    • 1970-01-01
    • 1970-01-01
    • 2020-08-16
    相关资源
    最近更新 更多