Hi,
I would like to suggest to have a method that returns the value of awaited flag in FlowVarBase just like isReady().
Usually when multiple threads return FlowVar values, you can check when their values are ready using isReady() method, and then do something with the value returned using ^ operator (which calls await()), you can call this operator multiple times without knowing if you already called and used the returned value, unless you keep a track somewhere, like an auxiliary array, which is an extra code that blot the code and logic flow.
The other scenario would be waiting for multiple thread to finish with different return types, again having an auxiliary array to keep track of the process is not ideal for the clarity and flow of the code and logic.
If awaited is exposed through a method like isRead() or isReturned() means the code can keep track of return values that has been read from within FlowVar itself.
Basically we can then have:
var tasks: seq[FlowVar[int]] = @[]
# ... spawn some tasks
while true:
for task in tasks:
if task.isReady() and not task.isRead():
echo ^task
# ... Break if all task.isRead() are true
I hope this makes sense and that I'm not confused about how threadpool works.
Other than that I'm enjoying working on a Nim project again!!!
Maybe like this? (untested)
while true:
let pos = awaitAny tasks
if pos == -1: break
echo ^(tasks[pos])
@mashingan15h Thanks, I'm aware of this pattern, awaitAny() works only for FlowVarBase, and it doesn't provide information whether if the FlowVar has been read or not.
The use case I have in mind is to check multiple FlowVar if they are ready and read to do something, something like, if tasks[1] is ready and not used yet then use it otherwise wait for another task to finish and use its value.
or maybe like this?
import threadpool
import sequtils
from os import sleep
proc mul5(x: int): int =
sleep(x * 1000)
x * 5
var tasks = newSeq[FlowVarBase]()
for i in 1 .. 4:
tasks.add(spawn mul5(i))
var count = 0
while count < tasks.len:
inc count
let pos = awaitAny tasks
if pos == -1: break
echo `^`(cast[FlowVar[int]](tasks[pos]))
using count instead of checking from returned -1, checked it and it didn't return -1 from awaitAny
it doesn't provide information whether if the FlowVar has been read or not
Is it something that must only be read once?