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

I got this code
Handler.jl

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

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

Do you want the @async macro perhaps?

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.

2 Likes

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.

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

    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

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

The mutable struct Self has no field “user”?