# What's the point of \`ntasks\` in \`asyncmap\`

**URL:** <https://discourse.julialang.org/t/whats-the-point-of-ntasks-in-asyncmap/99492>\
**Category:** General Usage\
**Created:** [May 27, 2023, 6:00pm UTC](https://discourse.julialang.org/t/whats-the-point-of-ntasks-in-asyncmap/99492 "2023-05-27T18:00:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [May 27, 2023, 6:00pm UTC](https://discourse.julialang.org/t/whats-the-point-of-ntasks-in-asyncmap/99492/1 "2023-05-27T18:00:42Z")

</div>

```julia
julia> function f(_...)
           s = 0.0
           for i = 1:10^9
               s = max(cos(i), sin(i))
           end
           return s
       end
f (generic function with 2 methods)

julia> asyncmap(f, 1:2)

```

I always assumed `asyncmap` never uses more than 100% \* 1 CPU thread, and the above example indeed never exceed 100% when looking at `top` command.

But the documentation is IMO confusion:

> asyncmap(f, c…; ntasks=0, batch\_size=nothing)  
> `ntasks` specifies the number of tasks to run concurrently. Depending on the length of the collections, if `ntasks` is unspecified, up to 100 tasks will be used for concurrent mapping.  
> `ntasks` can also be specified as a zero-arg function. In this case, the number of tasks to run in parallel is checked before processing every element and a new task started if the value of `ntasks_func` is greater than the current number of tasks.

I suspect the problem is that the word “concurrent” here is used under a super specific / narrow definition. Because this last bit of the doc actually describes the “will ever only use 100% \* 1 CPU”

> Currently, all tasks in Julia are executed in a single OS thread co-operatively. Consequently, `asyncmap` is beneficial only when the mapping function involves any I/O - disk, network, remote worker invocation, etc.

My “question” is, can we make the doc better? and what’s the point of having N tasks if they are all executed on a single OS thread anyway

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [May 27, 2023, 6:08pm UTC](https://discourse.julialang.org/t/whats-the-point-of-ntasks-in-asyncmap/99492/2 "2023-05-27T18:08:02Z")

</div>

its useful for IO, e.g. downloading a bunch of files or such, since julia can switch tasks while waiting on the network. You do want some control over the number of tasks because if there are too many, it’s possible there will be long delays between when the cpu can attend to a given task, which can cause errors, e.g. [AWSException: RequestTimeTooSkewed - s3.put\_object · Issue #598 · JuliaCloud/AWS.jl · GitHub](https://github.com/JuliaCloud/AWS.jl/issues/598#issuecomment-1372551170)

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [May 27, 2023, 6:08pm UTC](https://discourse.julialang.org/t/whats-the-point-of-ntasks-in-asyncmap/99492/3 "2023-05-27T18:08:15Z")

</div>

> [@jling](#):
>
> My “question” is, can we make the doc better? and what’s the point of having N tasks if they are all executed on a single OS thread anyway

@mkitti

> It runs the blocking operation on a different thread, it blocks that thread and then returns the result to the launching thread

ah right…this is for task-local libUV thread…ugh

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [May 27, 2023, 9:19pm UTC](https://discourse.julialang.org/t/whats-the-point-of-ntasks-in-asyncmap/99492/4 "2023-05-27T21:19:53Z")

</div>

For a specific example, where `ntasks` are very important, I quote myself from slack:

Try `asyncmap(_ -> run(`ls`), 1:10^4)` or with something longer-running than `ls`: it starts ntasks processes at the same time.
