I’d like to invoke a game-playing module’s “get_move” procedure to ask an AI player to make a move, but I don’t want to wait forever. The normal (no-timing) use would be something like this:
state2 = next_state state1 (get_move state1)
In my best-of-all-worlds, the “timed” use would look like this
let state2 =
match timed (fun () -> get_move state1) 4 with
| Timeout -> failwith "..."
| Success m -> next_state state1 m
where “timed”'s second argument is the number of seconds I’m willing to wait to get an answer.
It appears that
Async is probably the right thing to use here (although maybe it’s now
Lwt), but it’s sufficiently complex and general that I’m overwhelmed. Quickly scanning the chapter in Real World Ocaml suggests to me that the particular bit that I need may involve
Deferred.any, but that’s about as far as I got.
I’m assuming that the thing I’m looking for is some standard Ocaml idiom; if it’s not, I can hack my way around it with some shell-scripting for my particular application. On the other hand, if it IS a simple idiom, my Google-fu isn’t strong enough to find it, and I’d appreciate any help I can get. By the way, this doesn’t need to be perfect in any sense. “About 4 second of clock time” is all that I need here: if the timeout occurred after 3.5 or 4.5 clock-seconds, or after 4 CPU seconds, or anything at all like that…I’d be totally happy and could adjust the time-limit to my needs.