# How to know when a task is done?

**URL:** https://discourse.julialang.org/t/how-to-know-when-a-task-is-done/63978
**Category:** General Usage
**Tags:** question, task
**Created:** [July 3, 2021, 7:16am UTC](https://discourse.julialang.org/t/how-to-know-when-a-task-is-done/63978 "2021-07-03T07:16:11Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)
#### Post date: [July 3, 2021, 7:16am UTC](https://discourse.julialang.org/t/how-to-know-when-a-task-is-done/63978/1 "2021-07-03T07:16:11Z")

</div>

If I create a `Task` using `@async`, and want to do something (like changing some status) immediately after that task is done, how do I do that? I guess `istaskdone` can only let me check everytime manually. I don’t quite understand `notify` & `Condition`, do they just notify me, or I can run some functions?

---

<div class="post-metadata">

### Author: ![findmyway](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/findmyway/32/4946_2.png) [@findmyway](https://discourse.julialang.org/u/findmyway)
#### Post date: [July 3, 2021, 7:37am UTC](https://discourse.julialang.org/t/how-to-know-when-a-task-is-done/63978/2 "2021-07-03T07:37:33Z")

</div>

You can [`wait`](https://docs.julialang.org/en/v1/base/parallel/#Base.wait) the task to finish.

---

<div class="post-metadata">

### Author: ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)
#### Post date: [July 4, 2021, 7:20am UTC](https://discourse.julialang.org/t/how-to-know-when-a-task-is-done/63978/3 "2021-07-04T07:20:26Z")

</div>

How? I don’t want `wait` to block my following commands. What I want is scheduling a `Task` and then do my things. When the `Task` is done, it will notify me, or do more things, like writing some text to a file, etc.

---

<div class="post-metadata">

### Author: ![findmyway](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/findmyway/32/4946_2.png) [@findmyway](https://discourse.julialang.org/u/findmyway)
#### Post date: [July 4, 2021, 8:03am UTC](https://discourse.julialang.org/t/how-to-know-when-a-task-is-done/63978/4 "2021-07-04T08:03:40Z")

</div>

> [@singularitti](#):
>
> I don’t want `wait` to block my following commands. What I want is scheduling a `Task` and then do my things.

Then you can simply create another `Task`, inside of which you `wait` for the previous task.

---

<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: [July 4, 2021, 9:16am UTC](https://discourse.julialang.org/t/how-to-know-when-a-task-is-done/63978/6 "2021-07-04T09:16:40Z")

</div>

Also you can just add this command inside your task

```julia
@async begin
   # commands of your original task
    # do something, when previous commands are complete
end

```

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [July 4, 2021, 9:18am UTC](https://discourse.julialang.org/t/how-to-know-when-a-task-is-done/63978/7 "2021-07-04T09:18:21Z")

</div>

> [@singularitti](#):
>
> When the `Task` is done, it will notify me, or do more things, like writing some text to a file, etc.

What do you mean by “notify me”?

---

<div class="post-metadata">

### Author: ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)
#### Post date: [July 4, 2021, 5:47pm UTC](https://discourse.julialang.org/t/how-to-know-when-a-task-is-done/63978/10 "2021-07-04T17:47:48Z")

</div>

> What do you mean by “notify me”?

Sorry if I was unclear. I want a `Task` that can handle some jobs for me while not blocking what I am doing ( `@async` ). But when that `Task` is done, I can immediately know it instead of checking with `istaskdone` each time manually. Also, I may want it to change the status of the job from `Running` to `Succeeded` or `Failed` , etc. And I may also want to do some other things immediately when the `Task` is done.  
You can see my code [here](https://github.com/MineralsCloud/SimpleWorkflow.jl/blob/170b2b14f920e2f338f1f393d2052d7f001f1990/src/SimpleWorkflow.jl#L91-L101). I used `@spawn` to do this, but it is not the most simple way, I think.

---

<div class="post-metadata">

### Author: ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)
#### Post date: [July 4, 2021, 6:06pm UTC](https://discourse.julialang.org/t/how-to-know-when-a-task-is-done/63978/11 "2021-07-04T18:06:03Z")

</div>

Is this for use in an interactive Julia environment, or for a program to run by itself?

---

<div class="post-metadata">

### Author: ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)
#### Post date: [July 4, 2021, 6:23pm UTC](https://discourse.julialang.org/t/how-to-know-when-a-task-is-done/63978/12 "2021-07-04T18:23:27Z")

</div>

Both. I want it to be programmable as scripts and also interactive.

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [July 5, 2021, 7:39am UTC](https://discourse.julialang.org/t/how-to-know-when-a-task-is-done/63978/13 "2021-07-05T07:39:17Z")

</div>

The problem is that, even with your clarification, “notify me” is ill defined. You are not your program after all - there is no automatic message queue or notification bar or something like that, you’ll have to create something that handles finished tasks for you. This is what @findmyway meant when they mentioned you have to create another Task that handles those for you. One way to achieve that is to have each task you spawned take e.g. a Channel to put results, errors etc. into and have a number of other tasks pull results out of that channel and act according to whatever that result is (e.g. spawn new tasks, write them to a file, etc.)

If you want it to work interactively, I’d look into [ReplMaker.jl](https://juliahub.com/ui/Packages/ReplMaker/TRU6e/0.2.5). You will have to decouple the interactive part from the standalone part, else your code will probably beome a lot harder to maintain.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [July 5, 2021, 11:23am UTC](https://discourse.julialang.org/t/how-to-know-when-a-task-is-done/63978/14 "2021-07-05T11:23:16Z")

</div>

Something like

```julia
@sync begin
    c = Condition()
    @async begin
        for i in 1:5
            println("I am doing work in the background...")
            sleep(0.5)
        end
        notify(c)
    end

    @async begin
        wait(c)
        println("I got notified")
    end
end

```

with output:

```julia
I am doing work in the background...
I am doing work in the background...
I am doing work in the background...
I am doing work in the background...
I am doing work in the background...
I got notified

```

?
