Myid() does not work as expected in @everywhere

I’m running the following code in Visual Studio Code:

using Distributed, Plots
addprocs(3)

@everywhere begin
    using Plots
    x = [0, 300]
    y = [0, 300]
    p = scatter(x, y)
    title!(p, "Process: $(myid())")
    display(p)
end


@distributed for i = 1:300
    # title!(p, "Process: $(myid())")
    append!(p, (i, i))
    display(p)
end

My expectation is that this will show four plots, one for each process and that the plots will have different titles corresponding to the process id of each plot as determined by myid(). However, instead I get that the four plots each have the same title: “Process: 1”.

If I move the line with title! to the @distributed for loop, then I get the expected behavior. But why is it working this way?

That intepolation occurs first, “Process: $(myid())” in macro before calling serialization. That’s why you see 1 in all titles(check with macroexpand). string("Process: ", myid()) prevents that, I think.

Confirming the above answer, this works for me:

@everywhere begin
    using Plots; plotly()
    x = [0, 300]
    y = [0, 300]
    p = scatter(x, y)
    mid = myid()
    title!(p, "Process: $mid")
    display(p)
end

EDIT:
Referring to the below, I did not test the original version either.
I also have titles 1-4 when running the original code.

Actually, my theory did not work out. I see different numbers in titles. Which version of julia are you using? Mine was v1.5.2.

I am running v1.5.2. I also have recently updated the Plots package.

The plots momentarily show the expected title before changing to “Process: 1”.

I used the modified code below and I still see the behavior in my original post.

using Distributed, Plots
addprocs(3)

@everywhere begin
    using Plots; plotly()
    x = [0, 300]
    y = [0, 300]
    p = scatter(x, y)
    mid = myid()
    title!(p, "Process: $mid")
    # display(p)
end


@sync @distributed for i = 1:300
    # title!(p, "Process: $(myid())")
    append!(p, (i, i))
end

@everywhere begin
    display(p)
end