Created
April 16, 2018 13:14
-
-
Save allochi/b8450b6e0599c15ff0b455ccec1f09a7 to your computer and use it in GitHub Desktop.
Trying to understand Nim awaitAny()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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