Skip to content

Instantly share code, notes, and snippets.

@Arkoniak
Created August 2, 2021 17:59
Show Gist options
  • Save Arkoniak/bec856627c8851afe71920bff7a90950 to your computer and use it in GitHub Desktop.
Save Arkoniak/bec856627c8851afe71920bff7a90950 to your computer and use it in GitHub Desktop.
Multiple async tasks cancellation
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