1

我有以下从mirageOS github repo修改的代码块:

open Lwt.Infix

module Main (KV: Mirage_kv.RO) = struct

  let start kv =
    let read_from_file kv =
        KV.get kv (Mirage_kv.Key.v "secret") >|= function
            | Error e ->
                Logs.warn (fun f -> f "Could not compare the secret against a known constant: %a"
                KV.pp_error e)
            | Ok stored_secret ->
                Logs.info (fun f -> f "Data -> %a" Format.pp_print_string stored_secret);
               
    in
        read_from_file kv
end

此代码从名为“secret”的文件中读取数据并输出一次。我想不断地读取文件并从中输出,并在两者之间进行睡眠。

用例是这样的:当这个程序运行时,我会secret用其他进程更新文件,所以我想看看输出的变化。

我试过什么?

我试图将最后一条语句放在 while 循环中

in 
   while true do 
   read_from_file kv
   done

但它给出了错误This expression has type unit Lwt.t but an expression was expected to type unit 因为它位于 while 循环的主体中

我只知道 lwt 是一个线程库,但我不是 ocaml 开发人员,也不想成为其中的一员,(我对 MirageOS 很感兴趣),所以我找不到编写它的函数语法。

4

1 回答 1

3

您需要将循环编写为函数。例如

let rec loop () =
  read_from_file kv >>= fun () ->
  (* wait here? *)
  loop ()
in
loop ()
于 2019-11-15T13:15:41.120 回答