# Async limit

**URL:** <https://discourse.julialang.org/t/async-limit/62732>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [June 11, 2021, 12:30pm UTC](https://discourse.julialang.org/t/async-limit/62732 "2021-06-11T12:30:58Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [August 1, 2021, 5:49pm UTC](https://discourse.julialang.org/t/async-limit/62732/2 "2021-08-01T17:49:57Z")

</div>

It is reasonable to use a semaphore for this, as described here for threads:

> [@Control number of threads](https://discourse.julialang.org/t/control-number-of-threads/62887/3):
>
> Semaphore You can use what ever threading solution you want. (Including Threads.@threads ) plus a [Base.Semaphore](https://docs.julialang.org/en/v1/base/parallel/#Base.Semaphore) to control number of threads active at a time. Something like using Threads sem = Base.Semaphore(2) # at most 2 at a time Threads.@threads for ii in 1:100 Base.acquire(sem) println(threadid()) Base.release(sem) end This will ensure only 2 threads are doing anything at a time. THough which two may change (and in the case of Threads.@thread will because of how it all…

I have also seen a `Channel` used as a adhoc semaphore.  
Which is marginally faster since it is not having to take out locks etc.  
but probably not worth the loss of clarity for code.  
Plus using a semaphore frees you to change from tasks to threads later.  
(threads are just tasks without the sticky bit set)

---

_[View the full topic](https://discourse.julialang.org/t/async-limit/62732)._
