在OCaml中异步调用函数

在OCaml中异步调用函数,ocaml,Ocaml,我正在修改facebook/pfff工具 在graph_code_java.ml中,我在两个单独的文件中添加了详细信息(节点和边):Edges.json和GraphSON.json 最后,在执行完所有操作后,我调用函数cleanup_graphson,它合并了Edges.json和graphson.json 下面是我修改后graph_code_java的样子: let build ?(verbose=true) ?(only_defs=false) root files = let g =

我正在修改facebook/pfff工具

在graph_code_java.ml中,我在两个单独的文件中添加了详细信息(节点和边):Edges.json和GraphSON.json

最后,在执行完所有操作后,我调用函数cleanup_graphson,它合并了Edges.json和graphson.json

下面是我修改后graph_code_java的样子:

let build ?(verbose=true) ?(only_defs=false) root files =
  let g = G.create () in
  G.create_initial_hierarchy g;

  let lookup_fails = Common2.hash_with_default (fun () -> 0) in

(* step1: creating the nodes and 'Has' edges, the defs *)
...
(* step3: creating the 'Use' edges that can rely on recursive inheritance   *)
if verbose then pr2 "\nstep3: extract uses";
files +> Console.progress ~show:verbose (fun k ->
List.iter (fun file ->
 k();
 let readable = Common.readable ~root file in
 let ast = parse ~show_parse_error:false file in
 extract_defs_uses ~phase:Uses ~g ~ast ~readable ~lookup_fails;
));
end;
(* step 4: Merge Edges.json and GraphSON.json *)
if !write_to_graphson = true then
  begin
    pr "cleaning up";
    GS.cleanup_graphson;
  end;
g
函数cleanup_graphson如下所示:

let cleanup_graphson =
  pr "Cleaning up!";
  Common.append_file "GraphSON.json" "] \n";
  Common.append_file "Edges.json" "] \n";
  let str = Common.read_file "Edges.json" in
  Common.append_file "GraphSON.json" str
但是当我运行这个程序时,cleanup_graphson会在调用其他所有操作之前被调用(步骤1、步骤2、步骤3)。除此之外,所有其他步骤都按顺序运行。我不知道为什么会出现这种情况,是因为正在执行文件操作吗?还有其他人面临过这个问题吗

注意:Console.progress的代码是


谢谢你看我的问题

除非您专门使用多线程,否则基本上不可能异步执行任何代码


如果我对这段代码一无所知,我会怀疑缓冲区有问题。也许计算是按照您期望的顺序进行的,但是
pr
的输出比其他输出先出来。当程序使用两种截然不同的输出写入方式时,这种情况很容易发生。

cleanup\u graphson是一个变量,而不是一个函数。当我把它修改到下面时,它按顺序执行。我在graphson中添加了一个()作为参数

let cleanup_graphson ()=
  pr "Cleaning up!";
  Common.append_file "GraphSON.json" "] \n";
  Common.append_file "Edges.json" "] \n";
  let str = Common.read_file "Edges.json" in
  Common.append_file "GraphSON.json" str

感谢您的回复,我将cleanup_graphson定义为一个变量,而不是一个函数。