I’m a newbie in julia language, I’ve written a parallel code using Distributed, and I want to write the intermediate results of the program’s calculations to a file, this is my code.
using Distributed
# Set up parallel workers
addprocs(4)
@everywhere begin
function my_function(n)
# Compute the result
result=n;
# Open the file in append mode
f = open("output.txt", "a")
# # Write the result to the file
println(f, "Result: ", result)
close(f)
# open("output.txt","a",lock=true) do io
# write(io,"Result: "*string(result))
# write(io,"\r\n")
# end
return result
end
end
@distributed for i in 1:10000
result = my_function(i)
println("Result for iteration $i: $result")
end
I want get output.txt as follow:
Result: 1
Result: 2501
Result: 2
Result: 3
Result: 4
Result: 5
Result: 6
Result: 7
Result: 8
but I got:
Result: 2566
Result: 85
Result: 2567
Result: 86
Result: 2568
Result: 87
Result: 2569
Result: 88
0
Result: 2571
Result: 89
Result: 90
2
Result: 2573
Result: 7501
Result: 105
Result: 2587
Result: 107
I can`t understand.