Created
August 2, 2021 17:59
-
-
Save Arkoniak/bec856627c8851afe71920bff7a90950 to your computer and use it in GitHub Desktop.
Multiple async tasks cancellation
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
function foo() | |
ts = Task[] | |
begin | |
for i in 1:20 | |
t = @task begin | |
sleep(i/10) | |
println(i) | |
@assert i != 5 | |
end | |
push!(ts, t) | |
end | |
foreach(schedule, ts) | |
ht = @async begin | |
while true | |
all(istaskdone, ts) && break | |
if any(istaskfailed, ts) | |
for t in ts | |
istaskdone(t) && continue | |
istaskfailed(t) && continue | |
@async try Base.throwto(t, InterruptException()) catch end | |
yield() | |
end | |
break | |
end | |
yield() | |
end | |
end | |
wait(ht) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment