how can I simultaneously wait over 2 AsyncPipes?
It's a bit low level, but you can use or from asyncFutures
https://nim-lang.org/docs/asyncfutures.html#or%2CFuture%5BT%5D%2CFuture%5BY%5D
This is rough pseudo code, but you would use it something like this:
proc doingAsyncStuff {.async.} =
let pipeFuture1 = someOtherAsyncProc() # you would replace this with your async pipes.
let pipeFuture2 = someOtherAsyncProc()
# this will await until one of the futures is ready. (You can also use `and` if you want to wait for both)
await (pipeFuture1 or pipeFuture2)
# This is the low level stuff.
# we don't know which future actually finished, so we have to check manually.
if pipeFuture1.finished() :
let val = pipeFuture1.read()
# do stuff
else if pipeFuture2.finished() :
let val = pipeFuture2.read()
# do other stuff
Weee, seems with your awesome help I mostly got it to work! (At least on Windows; I'm planning to test on other OSes soon too.) Though it seems not perfect yet:
(a) If I use the code as presented, I seem to be losing part of the output from stderr:
O 10 some stdou
E 10 some stder
O 10 t
some std
E 10 r
some std
O 5 out2
E 5 err2
E 10 some stder
E 10 r3
some st
X 0
(notice missing derr4).
(b) If I change the code to the commented-out variants of lines (i.e. change the waitFor(fe or fo or fx) to await fe or fo or fx), the code hangs without reaching echo "X" (it seems to be a busy wait, as far as I checked once):
O 10 some stdou
E 10 some stder
O 10 t
some std
E 10 r
some std
O 5 out2
E 5 err2
E 10 some stder
(c) If I change the exit condition in the original (non-{.async.}) code to if fx.finished and fe.finished and fo.finished:, I was initially getting a segfault... but on second try now it's working perfectly OK (wtf?!?!?). Currently I assume this is probably the correct solution? I wonder if the segfault was just some other error I did... Or is this version still somehow incorrect indeed?
I don't really have a good mental model of what's going on inside the async/await machinery. Can someone possibly try to help me understand which solution is the best one, and what are the subtle issues with the others that are failing? and if it's possible to fix them somehow? I'd be very happy to learn some more about what's going on internally; I still have to build some more complex stuff on this, probably with AsyncQueue, so it would be useful to me if I managed to build a better mental model!
Anyway, thanks esp. @dom96 and @rayman22201 for all the great help already!