# "Partially" synchronizing asynchronous tasks

**URL:** <https://discourse.julialang.org/t/partially-synchronizing-asynchronous-tasks/39841>\
**Category:** General Usage\
**Tags:** question\
**Created:** [May 20, 2020, 4:02pm UTC](https://discourse.julialang.org/t/partially-synchronizing-asynchronous-tasks/39841 "2020-05-20T16:02:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![martincornejo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martincornejo/32/13436_2.png) [@martincornejo](https://discourse.julialang.org/u/martincornejo)\
**Post date:** [May 20, 2020, 4:02pm UTC](https://discourse.julialang.org/t/partially-synchronizing-asynchronous-tasks/39841/1 "2020-05-20T16:02:02Z")

</div>

The `@sync` / `@async` pair is very useful for waiting a series of asynchronous tasks to complete:

```julia
@sync for element in collection
   @async perform_task(element)
end

```

Is there a way to do something similar, but instead of waiting for all tasks to complete it is only necessary to wait for the completion of a certain number of tasks before continuing?

Reading the Tasks section of the documentation I found things like `istaskdone`, `wait`, `notify` and `Condition` but couldn’t figure out how they work and if they are implementable for this purpose.

---

<div class="post-metadata">

**Author:** ![martincornejo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martincornejo/32/13436_2.png) [@martincornejo](https://discourse.julialang.org/u/martincornejo)\
**Post date:** [May 20, 2020, 4:27pm UTC](https://discourse.julialang.org/t/partially-synchronizing-asynchronous-tasks/39841/2 "2020-05-20T16:27:55Z")

</div>

What I can do for now is:

```julia
task = Array{Task}(undef, length(collection))

for (i, el) in enumerate(collection)
    task[i] = @async perform_task(el)
end

while sum(istaskdone.(task)) < n_tasks
    sleep(0.1)
end

```

Any recommendations on something better than `sleep`?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [May 20, 2020, 5:29pm UTC](https://discourse.julialang.org/t/partially-synchronizing-asynchronous-tasks/39841/3 "2020-05-20T17:29:22Z")

</div>

The “synchronize by communicating” approach would be for each task to send a signal across a shared channel when it’s done and have a single task reading those signals and doing something once a certain number of them have been received.
