concurrency - 在 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
.
解决方案
该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
.
推荐阅读
- javascript - 将 fadeOut() 限制为每秒 3 步
- bash - bash相对路径到绝对路径
- angular - 组件scss文件导入样式scss文件时,Angular组件的样式视图封装不起作用
- c++ - CS50 - C++:“使用命名空间标准;”的问题
- amazon-web-services - AWS 无服务器身份池角色附件
- python - 我的代码返回 HTTP 错误 403:禁止
- r - R中行级别连续零的复杂数据计算(滞后v / s领先)
- javascript - 如何使用 CSS 模块在 React 中设置子组件的样式
- php - Laravel 多重身份验证返回 JSON
- npm - 错误:无法解析 .... papaparse 中的“流”