# What is the recommended way to run N workers asynchronously?

**URL:** <https://discourse.julialang.org/t/what-is-the-recommended-way-to-run-n-workers-asynchronously/63128>\
**Category:** General Usage\
**Tags:** question, performance\
**Created:** [June 18, 2021, 7:43am UTC](https://discourse.julialang.org/t/what-is-the-recommended-way-to-run-n-workers-asynchronously/63128 "2021-06-18T07:43:13Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [June 18, 2021, 8:47am UTC](https://discourse.julialang.org/t/what-is-the-recommended-way-to-run-n-workers-asynchronously/63128/2 "2021-06-18T08:47:34Z")

</div>

You can use “Task farm” pattern from this [tutorial](https://discourse.julialang.org/t/tutorial-concurrency-patterns-for-controlled-parallelisms/62651).

```julia
using HTTP, JSON3
using Base.Threads: @spawn

umap(f, xs; kwargs...) = umap(f, Any, xs; kwargs...)
function umap(f, TY::Type, xs::Channel; ntasks = Threads.nthreads(), buffersize = ntasks)
    return Channel{TY}(buffersize) do ys
        @sync for i in 1:ntasks
            @spawn for x in xs
                put!(ys, f(i, x))
            end
        end
    end
end

function calc(nworkers = 10, nreqs = 100)
    xs = Channel{Int}(Inf)
    for _ in 1:nreqs
        put!(xs, 1)
    end
    close(xs)

    res = umap(Int, xs; ntasks = nworkers, buffersize = nreqs) do name, n
        println("worker-$name")
        response = HTTP.request("GET", "https://qrng.anu.edu.au/API/jsonI.php?length=$(n)&type=uint16")
        json = JSON3.read(String(response.body))
        sum(json["data"])
    end

    collect(res)
end

calc() # long list of worker name and vector of numbers in the end.

```

Here `@spawn` is used, which can be an overkill, you can change it to `@async`, but it really doesn’t matter in this case.

---

_[View the full topic](https://discourse.julialang.org/t/what-is-the-recommended-way-to-run-n-workers-asynchronously/63128)._
