Skip to content

Instantly share code, notes, and snippets.

@allochi
Created April 16, 2018 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allochi/b8450b6e0599c15ff0b455ccec1f09a7 to your computer and use it in GitHub Desktop.
Save allochi/b8450b6e0599c15ff0b455ccec1f09a7 to your computer and use it in GitHub Desktop.
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