# Construct already completed task

**URL:** <https://discourse.julialang.org/t/construct-already-completed-task/95577>\
**Category:** General Usage\
**Tags:** task\
**Created:** [March 5, 2023, 3:03pm UTC](https://discourse.julialang.org/t/construct-already-completed-task/95577 "2023-03-05T15:03:35Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)\
**Post date:** [March 5, 2023, 3:03pm UTC](https://discourse.julialang.org/t/construct-already-completed-task/95577/1 "2023-03-05T15:03:35Z")

</div>

What’s the best way to construct a `Task` which has already completed? My current best shot is

```julia
t = @async nothing
wait(t)

```

but this feels both clumsy and inefficient.

The use case is that I would like to have a background task with the following behaviour.

- Start when work comes in and there isn’t already a background task running.
- Keep running while there’s work, then shut down once there’s no more work left.

In somewhat pseudo-code-ish Julia, this can be achieved as follows.

```julia
# Initialize
work_queue = []
t = task_which_is_already_done()

# When work comes in
push!(work_queue, work)
if istaskdone(t)
    t = @async begin
        while !isempty(work_queue)
            process(pop!(work_queue))
        end
    end
end

```
