# How many async tasks are still alive

**URL:** https://discourse.julialang.org/t/how-many-async-tasks-are-still-alive/22668
**Category:** General Usage
**Tags:** question
**Created:** [April 3, 2019, 1:09am UTC](https://discourse.julialang.org/t/how-many-async-tasks-are-still-alive/22668 "2019-04-03T01:09:14Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)
#### Post date: [April 3, 2019, 1:09am UTC](https://discourse.julialang.org/t/how-many-async-tasks-are-still-alive/22668/1 "2019-04-03T01:09:14Z")

</div>

Is there a way to know at any one time in a session how many async threads (if any) are still running and (ideally) be able to recover the corresponding tasks as a variable we can use in a `schedule(task, ...)`?

I’m aware of `current_task()` but would ideally like a `all_running_tasks()` or similar.

Here’s a contrived example:

```julia-repl
julia> ping() = while true println("ping"); sleep(5) end
julia> pong() = while true println("pong"); sleep(5) end
julia> @async ping()
julia> @async pong()
# ... ping and pong printed
julia> get_all_running_tasks() # the function I'd like
2-element Array{Task,1}:
Task ...
Task ...

```

Note: I could write `tsk1 = @async ...` and `tsk2 = @async ...` and then inspect those but for the purpose of this question, please assume I can’t do that.

Thanks!

---

<div class="post-metadata">

### Author: ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)
#### Post date: [April 3, 2019, 5:23am UTC](https://discourse.julialang.org/t/how-many-async-tasks-are-still-alive/22668/2 "2019-04-03T05:23:01Z")

</div>

i didn’t find any way ☹ ,from what i read in the code, the scheduler is outside julia, so accessing it seems very difficult. an option is to register when a task starts an then finishes the assigned work in a data structure (an array or a dict)

---

<div class="post-metadata">

### Author: ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)
#### Post date: [April 3, 2019, 5:56am UTC](https://discourse.julialang.org/t/how-many-async-tasks-are-still-alive/22668/3 "2019-04-03T05:56:31Z")

</div>

Thanks; I guess I was wondering whether `libuv` could be coerced to provide this information. E.g. `uv_print_active_handles` or `uv_walk` seem promising but I know next to nothing about `libuv` so would appreciate a more informed opinion + how to call them if appropriate.

---

<div class="post-metadata">

### Author: ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)
#### Post date: [April 3, 2019, 6:02am UTC](https://discourse.julialang.org/t/how-many-async-tasks-are-still-alive/22668/4 "2019-04-03T06:02:47Z")

</div>

i was playing with `StackTraces.lookup.(backtrace())` , and it gives a line where the c call to start\_task is called, so maybe you can use this to access where and what task was inicialized

---

<div class="post-metadata">

### Author: ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)
#### Post date: [April 3, 2019, 6:08am UTC](https://discourse.julialang.org/t/how-many-async-tasks-are-still-alive/22668/5 "2019-04-03T06:08:13Z")

</div>

I don’t think there’s any official way to do this. There’s the internal `Base.Workqueue`, but I think this is a temporary holding area for tasks used before they make it into the low level libuv event system.

The internals of this stuff are still changing as the compiler guys work toward better concurrency and there were some PRs merged into 1.2 which change various internals, eg [https://github.com/JuliaLang/julia/pull/30838](https://github.com/JuliaLang/julia/pull/30838)

---

<div class="post-metadata">

### Author: ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)
#### Post date: [April 3, 2019, 6:18am UTC](https://discourse.julialang.org/t/how-many-async-tasks-are-still-alive/22668/6 "2019-04-03T06:18:12Z")

</div>

Thanks Chris, yeah Base.Workqueue is not useful here. I’ve had a look at the PR but I’m not sure it does what I want (but it’s likely I understand it only very partially) 😕

---

<div class="post-metadata">

### Author: ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)
#### Post date: [April 3, 2019, 7:00am UTC](https://discourse.julialang.org/t/how-many-async-tasks-are-still-alive/22668/7 "2019-04-03T07:00:43Z")

</div>

Yes I didn’t mean that PR would help you (sorry). Just pointing out that this stuff is still changing quite a bit internally so the details aren’t settled.

Could I ask about your use case which requires getting a list of tasks? There has been [some](https://github.com/JuliaLang/julia/issues/6283) [discussion](https://discourse.julialang.org/t/schedule-considered-harmful/10540/1) of using the emerging ideas of “structured concurrency” (described eg, [here](https://vorpus.org/blog/notes-on-structured-concurrency-or-go-statement-considered-harmful/)) to rework the way that task lifetimes are managed. I wonder if that would help your use case?

---

<div class="post-metadata">

### Author: ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)
#### Post date: [April 3, 2019, 7:13am UTC](https://discourse.julialang.org/t/how-many-async-tasks-are-still-alive/22668/8 "2019-04-03T07:13:36Z")

</div>

Sure, so in HTTP.jl I came accross a sneaky bug whereby calling `close(server)` (the recommended way to switch off a `@async HTTP.listen ...` loop) does in fact leave `@async ` tasks running potentially until the Julia session is terminated because they are left unaware of the fact that the server was closed.

I suggested a [simple fix](https://github.com/JuliaWeb/HTTP.jl/pull/406) for this but it made me realise that this could potentially happen in other places where `@async` threads may be used and where there may not be an explicit check that they’re properly terminated.

Having some way of verifying that at a given point there are no such threads hanging about, or if there are, being able to interrupt them by throwing or scheduling an error would, I think, be great (or, in some future where that exists, call `interrupt(task)`).

**Edit** : the pointers are appreciated, thanks!

---

<div class="post-metadata">

### Author: ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)
#### Post date: [April 3, 2019, 7:25am UTC](https://discourse.julialang.org/t/how-many-async-tasks-are-still-alive/22668/9 "2019-04-03T07:25:32Z")

</div>

Thanks, this sounds like _exactly_ the kind of problem that structured concurrency is designed to solve. You might find some of the discussion of robust cancellation interesting over at the [Trio forum](https://trio.discourse.group/c/structured-concurrency).

As I understand it, the main idea is to scope the lifetime of child tasks along with the function call stack so that tasks started by a function don’t outlive that function. (More generally, to be explicit about the context in which a task is started so that the task doesn’t outlive that context. Trio calls those contexts “nurseries”.)

---

<div class="post-metadata">

### Author: ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)
#### Post date: [April 3, 2019, 7:27am UTC](https://discourse.julialang.org/t/how-many-async-tasks-are-still-alive/22668/10 "2019-04-03T07:27:28Z")

</div>

That makes sense thanks, I was just looking over Trio, seems pretty cool and I guess potentially this relates to [https://github.com/JuliaLang/julia/issues/6283](https://github.com/JuliaLang/julia/issues/6283) .

---

<div class="post-metadata">

### Author: ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)
#### Post date: [April 3, 2019, 7:29am UTC](https://discourse.julialang.org/t/how-many-async-tasks-are-still-alive/22668/11 "2019-04-03T07:29:02Z")

</div>

Yes, njsmith on that github thread is the author of Trio.
