# I got a code that blocks the rest of the code , what can I do about it?

**URL:** <https://discourse.julialang.org/t/i-got-a-code-that-blocks-the-rest-of-the-code-what-can-i-do-about-it/15633>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 28, 2018, 6:17pm UTC](https://discourse.julialang.org/t/i-got-a-code-that-blocks-the-rest-of-the-code-what-can-i-do-about-it/15633 "2018-09-28T18:17:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheOnlyArtz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theonlyartz/32/12685_2.png) [@TheOnlyArtz](https://discourse.julialang.org/u/TheOnlyArtz)\
**Post date:** [September 28, 2018, 6:17pm UTC](https://discourse.julialang.org/t/i-got-a-code-that-blocks-the-rest-of-the-code-what-can-i-do-about-it/15633/1 "2018-09-28T18:17:03Z")

</div>

I got this code  
**Handler.jl**

```julia
module WSHandler
    include("Logger.jl")
    include("../Structs/User.jl")

    using Base
    using Dates
    import .WSLogger
    import .User
    import JSON

    function handleEvent(data, mainClient) # content is the key "d"
        eventName = data["t"]
        content = data["d"]
        #println(content)

        if eventName == "READY"
            user = content["user"]
            username = user["username"]
            id = user["id"]
            discriminator = user["discriminator"]
            avatar = user["avatar"]
            bot = user["bot"]
            mfa_enabled = user["mfa_enabled"]
            locale = user["locale"]
            verified = user["verified"]
            email = user["email"]

            println("WORKES")
            
            userObject = User.Self(user, username, id, discriminator, avatar, bot, mfa_enabled, locale, verified, email)

            println("BLOCKED and never executs")
            WSLogger.log("Client is ready as: $username", "Log")

            mainClient.send(eventName)
    end
  end
end

```

And as you can see the userObject blocks the rest of the code and exists without an error and with the output of “WORKS” since I’m printing it when it not blocking

**user.jl**

```julia
module User

    mutable struct Self
        username::String
        id::String
        discriminator::String
        avatar::String
        bot::Bool
        mfa_enabled::Bool
        locale::String
        verified::Bool
        email::Any
    end

    # Returns a Dictionary of the supplied User
    function serialize(user)
        serialized = Dict()
        for i in fieldnames(Self)
            value = getfield(user, i)
            serialized[i] = value
        end
        return serialized
    end
end

```

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [September 28, 2018, 6:36pm UTC](https://discourse.julialang.org/t/i-got-a-code-that-blocks-the-rest-of-the-code-what-can-i-do-about-it/15633/2 "2018-09-28T18:36:57Z")

</div>

Do you want the `@async` macro perhaps?

```julia
julia> @time @sync begin
           for i = 1:10
               @async begin
                   println("Before sleep [$i]")
                   sleep(1)
                   println("After sleep [$i]")
               end
           end
       end
Before sleep [1]
Before sleep [2]
Before sleep [3]
Before sleep [4]
Before sleep [5]
Before sleep [6]
Before sleep [7]
Before sleep [8]
Before sleep [9]
Before sleep [10]
After sleep [1]
After sleep [2]
After sleep [3]
After sleep [4]
After sleep [5]
After sleep [6]
After sleep [7]
After sleep [8]
After sleep [9]
After sleep [10]
  1.013503 seconds (4.36 k allocations: 226.700 KiB)

```

This code creates an async task for 10 different blocks which each print, sleep and print again, but all run “concurrently” in the I/O sense in that they do not block each other (as compared to the concurrent computation sense). The `@sync` macro blocks until all the `@async` tasks are done.

---

<div class="post-metadata">

**Author:** ![TheOnlyArtz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theonlyartz/32/12685_2.png) [@TheOnlyArtz](https://discourse.julialang.org/u/TheOnlyArtz)\
**Post date:** [September 28, 2018, 6:38pm UTC](https://discourse.julialang.org/t/i-got-a-code-that-blocks-the-rest-of-the-code-what-can-i-do-about-it/15633/3 "2018-09-28T18:38:42Z")

</div>

I don’t get what I need to do in order so the code won’t get blocked, adding `@async` before the  
`User.Self(user, username, id, discriminator, avatar, bot, mfa_enabled, locale, verified, email)`  
Didn’t work.

---

<div class="post-metadata">

**Author:** ![TheOnlyArtz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theonlyartz/32/12685_2.png) [@TheOnlyArtz](https://discourse.julialang.org/u/TheOnlyArtz)\
**Post date:** [September 28, 2018, 6:40pm UTC](https://discourse.julialang.org/t/i-got-a-code-that-blocks-the-rest-of-the-code-what-can-i-do-about-it/15633/4 "2018-09-28T18:40:17Z")

</div>

I didn’t mention this but `handler` is being called from other file:

```julia
    function eventLoop(connection::OpenTrick.IOWrapper, mainClient)
        while isopen(connection)
            parsedData = connection |> read |> String |> JSON.parse
            WSLogger.log("Received $(parsedData["t"]) with OPCode $(parsedData["op"])", "Log")

            if parsedData["op"] != 11
                @async WSHandler.handleEvent(parsedData, mainClient)
            end
        end
    end

```

As you can see I’m calling async on handleEvent

---

<div class="post-metadata">

**Author:** ![TheOnlyArtz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theonlyartz/32/12685_2.png) [@TheOnlyArtz](https://discourse.julialang.org/u/TheOnlyArtz)\
**Post date:** [September 28, 2018, 6:47pm UTC](https://discourse.julialang.org/t/i-got-a-code-that-blocks-the-rest-of-the-code-what-can-i-do-about-it/15633/5 "2018-09-28T18:47:20Z")

</div>

Update, calling `@async` on that worked, but I’m getting Task (Failed)

---

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [September 29, 2018, 7:59am UTC](https://discourse.julialang.org/t/i-got-a-code-that-blocks-the-rest-of-the-code-what-can-i-do-about-it/15633/6 "2018-09-29T07:59:01Z")

</div>

The mutable struct Self has no field “user”?
