OCaml 异步编译

OCaml Async compilation

我有这个代码

open Async.Std;;

let () = print_string "Hello World\n";;

并用

编译
ocamlfind ocamlopt -linkpkg -package async -thread ./hello.ml

stdout 是空的...但是如果代码只是

let () = print_string "Hello World\n";;

和编译命令

ocamlfind ocamlopt -linkpkg -package async -thread ./hello.ml

我在标准输出中有我的 "Hello world"。我做错了什么?为什么第一个示例不起作用?

它不起作用,因为 open Async.Std 隐式覆盖 I/O 函数,如 print_string 并使它们异步。因此,在您启动调度程序之前,它们不会被执行。

open Async.Std

let () =
  print_string "Hello World\n";              (* schedule "Hello World\n" printing *)
  don't_wait_for (exit 0);                   (* schedule exit from the program *)
  Core.Std.never_returns (Scheduler.go ())   (* run the scheduler *)

阅读有关异步的更多信息there