ITADN

World age problems when using @everywhere in a function

#156Openjosia-pool 创建于 2025-12-08
J
josia-poolcommented
I have a julia file that is structured something like this: ```julia using Distributed function main() @everywhere variable = [0, 1, 2, 3, 4, 5] for x in variable println(x^2) end end main() ``` From my understanding `@everywhere` should define the variable on every worker, including the main process. But when I run the code, it give me this error: ```UndefVarError: `variable` not defined in `Main` The binding may be too new: running in world age 38716, while current world is 38717.``` In a jupyter notebook, I can run main() again a second time, and then it works, since I assume then the world age matches... Can someone explain what is going on and if there is some way to circumvent this problem? I can fix it by defining it again in the main process `variable = [0, 1, 2, 3, 4, 5]`, or by taking the code out of the main() function but to me that doesn't feel intended.
0 条评论