首页 > 解决方案 > 在 OCaml 中使用 Lwt 时如何进行循环中断

问题描述

我正在编写代码来监视文件的内容。当程序到达文件末尾时,我希望它干净地终止。

let log () : input_channel Lwt.t = 
  openfile "log" [O_RDONLY] 0 >>= fun fd -> 
  Lwt.return (of_fd input fd);;

let rec loop (ic: input_channel) = Lwt_io.read_line ic >>= fun text -> 
    Lwt_io.printl text >>= fun _ -> loop ic;;

let monitor () : unit Lwt.t = log () >>= loop;;

let handler : exn -> unit Lwt.t = fun e -> match e with
    | End_of_file -> let (p: unit Lwt.t), r = Lwt.wait() in p
    | x -> Lwt.fail x;;

let main () : unit Lwt.t = Lwt.catch monitor handler;;

let _ = Lwt_main.run (main ());;

但是,当读取文件并到达末尾时,程序并没有终止,它只是挂起,我必须用 Ctrl+c 转义。我不确定 bind 的幕后发生了什么,但我认为无论它在做什么,最终Lwt_io.readline ic都应该到达文件末尾并返回一个End_of_file异常,这可能会被传递给处理程序等。

如果我不得不猜测一个决议,我想也许在定义的最后一个绑定中>>=我会包括一些if检查。但我想,我会检查是否Lwt_io.read_line返回End_of_file,我认为应该由handler.

标签: concurrencyocamlocaml-lwt

解决方案


Lwt.wait函数创建一个只能使用返回对的第二个元素解析的承诺,基本上,这个函数永远不会终止:

let never_ready () = 
  let (p,_) = Lwt.wait in
  p

这正是你所写的。

关于优雅终止,理想情况下,您应该在loop函数中执行此操作,以便您可以关闭通道并防止有价值资源的泄漏,例如,

let rec loop (ic: input_channel) = 
  Lwt_io.read_line ic >>= function
  | exception End_of_file -> 
    Lwt.close ic
  | text->
    Lwt_io.printl text >>= fun () -> 
    loop ic

但是,对您的代码的最小更改是使用Lwt.return ()而不是Lwt.wait在您的handler.


推荐阅读