local function connect_to(host,port,result)
result[host] = tcp.connect(host,port)
end
local result = {}
local tasks = nursery()
tasks:spawn(connect_to,'192.168.1.10',80,result)
tasks:spawn(connect_to,'fc00::1:6',80,result)
tasks:wait_any()
Don't you have to wait for them to complete in order to
get their results, and then use those to produce the return value or as
input to a subsequent computation?
In the example above, we don't want to wait for all of them to complete,
we only care about one of them completing.
And if that is not the case, then I
would argue that those tasks are not essential, and could just as well be
cancelled upon issuing the return statement (which causes control flow to
leave the current scope).
If we insist on waiting for the tasks to complete whenever we leave the
scope for non-error reasons, then we also have an asymmetry/inconsistency
of behavior when leaving the scope; i.e., the fate of the tasks depends on
the precise way in which we leave the scope, which I think makes programs
harder to reason about in a way.
My understanding of this is that when you have (at least from what I've
seen so far):
do
local tasks <close> = nursury()
nursury:spawn(...)
nursury:spawn(...)
nursory:spawn(...)
-- do some other stuff, that might error
end
Is that the program won't leave the scope until all the spawned coroutins
have finished doing what they do, unless you have explicitly cancelled them
before you leave the scope.
-spc