Instantly share code, notes, and snippets.

Embed
What would you like to do?
Trying to understand Nim awaitAny()
import threadpool, os, strformat
proc timer(d: int): int =
echo fmt"sleeping {d}"
sleep(d)
echo fmt"done {d}"
return d
var durations = [1000, 2000, 3000, 4000, 5000]
var tasks: seq[FlowVarBase] = @[]
var results: seq[int] = @[]
for i in 0 .. durations.high:
tasks.add spawn timer(durations[i])
var index = awaitAny(tasks)
while index != -1:
results.add ^cast[FlowVar[int]](tasks[index])
tasks.del(index)
echo repr results
index = awaitAny(tasks)
sync()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment