# Create multiple tasks in a loop

**URL:** https://discourse.julialang.org/t/create-multiple-tasks-in-a-loop/71701
**Category:** General Usage
**Tags:** multithreading, task
**Created:** [November 18, 2021, 6:18am UTC](https://discourse.julialang.org/t/create-multiple-tasks-in-a-loop/71701 "2021-11-18T06:18:41Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Bossax](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bossax/32/29872_2.png) [@Bossax](https://discourse.julialang.org/u/Bossax)
#### Post date: [November 18, 2021, 6:18am UTC](https://discourse.julialang.org/t/create-multiple-tasks-in-a-loop/71701/1 "2021-11-18T06:18:41Z")

</div>

Hi everyone,

I am trying to create multiple tasks in a forr loop using @eval. Here is a block of code

```julia
f(k) = begin sleep(1)
     println("t$k is done")
 end

begin
    for k in 1:4
         @eval $(Symbol(:t,k)) = @task f(k)
    end
    sleep(1)
    for k in 1:4
        @eval schedule($(Symbol(:t,k)))
    end
end

```

I want to assing the value k into the f() function when creating a task However, Julia throws back error

```julia
UndefVarError: k not defined

```

The error is caused by this line

```julia
@eval $(Symbol(:t,k)) = @task f(k)

```

How to correct this?

Any help would be appreciated!

Sitthichat

---

<div class="post-metadata">

### Author: ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)
#### Post date: [November 18, 2021, 8:39am UTC](https://discourse.julialang.org/t/create-multiple-tasks-in-a-loop/71701/2 "2021-11-18T08:39:53Z")

</div>

Is there a particular reason why you’d want to have your tasks in separate `t1`, `t2`, … variables instead of putting them in an array and referencing them as `t[1]`, `t[2]`, …?

If it works for you, an array-based solution would IMO be more Julian; it could look like this:

```julia
julia> function f(k)
           sleep(1)
           println("task $k is done")
       end
f (generic function with 1 method)

julia> tasks = [@task f(k) for k in 1:4]
4-element Vector{Task}:
 Task (runnable) @0x00007f10aa7b70a0
 Task (runnable) @0x00007f10aa7b71f0
 Task (runnable) @0x00007f10aa7b7340
 Task (runnable) @0x00007f10aa7b7490

julia> sleep(1)

julia> for t in tasks
           schedule(t)
       end

task 1 is done
task 2 is done
task 3 is done
task 4 is done

```

---

<div class="post-metadata">

### Author: ![wormholestds9](https://avatars.discourse-cdn.com/v4/letter/w/c6cbf5/32.png) [@wormholestds9](https://discourse.julialang.org/u/wormholestds9)
#### Post date: [November 18, 2021, 9:03am UTC](https://discourse.julialang.org/t/create-multiple-tasks-in-a-loop/71701/3 "2021-11-18T09:03:23Z")

</div>

You could insert $ in front of k so that it should be f($k).  
It worked for me.

@eval $(Symbol(:t,k)) = @task f($k)

---

<div class="post-metadata">

### Author: ![Bossax](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bossax/32/29872_2.png) [@Bossax](https://discourse.julialang.org/u/Bossax)
#### Post date: [November 19, 2021, 1:50am UTC](https://discourse.julialang.org/t/create-multiple-tasks-in-a-loop/71701/5 "2021-11-19T01:50:29Z")

</div>

Indeed! This looks more Julian. Thanks so much

---

<div class="post-metadata">

### Author: ![wormholestds9](https://avatars.discourse-cdn.com/v4/letter/w/c6cbf5/32.png) [@wormholestds9](https://discourse.julialang.org/u/wormholestds9)
#### Post date: [November 19, 2021, 5:18pm UTC](https://discourse.julialang.org/t/create-multiple-tasks-in-a-loop/71701/6 "2021-11-19T17:18:32Z")

</div>

You can even collapse them into a single instruction:

schedule.(@task f(k) for k=1:4)  
or  
foreach(schedule, (@task f(k) for k=1:4))

---

<div class="post-metadata">

### Author: ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)
#### Post date: [November 19, 2021, 7:54pm UTC](https://discourse.julialang.org/t/create-multiple-tasks-in-a-loop/71701/7 "2021-11-19T19:54:18Z")

</div>

In order to create a task and schedule it immediately (which was not the case in the OP), it is more direct to use `@async`:

```julia
tasks = [@async f(k) for k in 1:n]

```
