# @sync usage and better documentation for Asynchronous Programming

**URL:** <https://discourse.julialang.org/t/sync-usage-and-better-documentation-for-asynchronous-programming/51410>\
**Category:** New to Julia\
**Tags:** task\
**Created:** [December 7, 2020, 4:03pm UTC](https://discourse.julialang.org/t/sync-usage-and-better-documentation-for-asynchronous-programming/51410 "2020-12-07T16:03:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![KaarthikSundar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kaarthiksundar/32/20044_2.png) [@KaarthikSundar](https://discourse.julialang.org/u/KaarthikSundar)\
**Post date:** [December 7, 2020, 4:03pm UTC](https://discourse.julialang.org/t/sync-usage-and-better-documentation-for-asynchronous-programming/51410/1 "2020-12-07T16:03:33Z")

</div>

I am trying to understand the usage of @sync and @async macros in Julia. I am trying to get this MWE to work and the program does not terminate. Any help is appreciated.

```julia
function process_node(nodes, id)
    @show id
    sleep(1.0)
    nodes[id] = true 
    return
end 

function main() 
    nodes = Dict( i => false for i in 1:10 )
    jobs = Channel{Int}(15)
    for i in 1:10 
        put!(jobs, i)
    end 
    @sync for id in jobs
        @async process_node(nodes, id)
    end 
    println("done")
end 

main()

```

The program never gets to the line `println("done")`. I do not know why.

Also, if you can point me to some reference or blog that has a tutorial for using co-routines without talking about any distributed programming, it would be great.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [December 7, 2020, 7:50pm UTC](https://discourse.julialang.org/t/sync-usage-and-better-documentation-for-asynchronous-programming/51410/2 "2020-12-07T19:50:50Z")

</div>

```julia
for i in 1:10
    put!(jobs, i)
end
close(jobs)

```

The `for id in jobs` loop doesn’t exit until the channel is **empty** and **closed**. So you need to close the channel for the loop to complete. Since closing the channel _before_ you start the loop may seem weird, you could do something like:

```julia
jobs = Channel{Int}(5)

@async begin
    for i in 1:10
        put!(jobs, i)
    end
    close(jobs)
end

```
