According to the documentation, fetch()
will - Wait for a [`Task’] (Tasks · The Julia Language) to finish, then return its result value.
For the code and its output given below, I have the following questions -
- How does
isready(task1)
, written immediately below spawning of task1, not return false? Instead it waits for task1 to finish and then printsTrue
. Why is that? How can I achieve the functionality to test at a point whether a spawned function is currently over or not without forcing it to get over at that point? - Can I be sure that any line written below fetch(task1) will not be executed before task1 is complete?
using TickTock
using Distributed
# julia -p auto includes Distributed and
# adds number of workers = julia -p `nproc` on Linux
# julia -p %NUMBER_OF_PROCESSORS% on Windows
@everywhere function f1(x)
for i in 1:10000000000
x = 2*x + 1 - x
# if(i%1000000000 == 0)
# println(x)
# end
end
println(x)
end
tick()
task1 = @spawn f1(101.12)
println(isready(task1))
task2 = @spawn f1(102.12)
task3 = @spawn f1(103.12)
fetch(task1)
print("task1 fetched. Now fetching task2 and task3, akin to wait(task1)")
fetch(task2)
fetch(task3)
tock()
The output received is given below
[ Info: started timer at: 2024-04-11T01:32:04.573
From worker 2: 1.0000000101119999e10
true #Output for is task ready
task1 fetched. Now fetching task2 and task3, akin to wait(task1)
From worker 4: 1.0000000103119999e10
From worker 3: 1.0000000102119999e10
[ Info: 95.472729208s: 1 minute, 35 seconds, 472 milliseconds ```