2

How to do the following in an async way in Julia:

the JavaScript-ish pseudo-code would be:

for i in 1:100
    asyncCall(get(webpageAddr[i]), callback(err, data){
        insertToDB(db, coll, data)
    })
end

I am interested in how the async part of the code should look like (no so much the web query and db access, I'll take care of those). The 100 queries can run independently (asynchronously) from one another and the rest of the Julia code.

1 Answer 1

2

Julia prefers to write code in a synchronous way without losing the power of parallelism, so to speak. You do not have to think in terms of callbacks.

Below, you see @async macro, which runs task (coroutine) on the local process only and @sync macro to wait until all the enclosed tasks are done.

@sync begin 
    for i=1:10
        @async begin
            println("I mimic the get for $i"); # -> get(webpageAddr[i])
            sleep(rand(1:3)) #waiting for server answer at random number of seconds
            println("I mimic the callback for $i") # -> callback(err, data){insertToDB(db, coll, data}
        end
    end
end

I encouraged you to dig into the details of parallel computing in Julia, because these are enormous.

Sign up to request clarification or add additional context in comments.

1 Comment

Really great answer. Thanks. I especially like how you mimic the time it takes the get with sleep(rand), which makes it crystal clear how async works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.