Animation with threads in Plots.jl?

Since the canonical way of making animation involes:

anim = @animate for i in some_range
            plotting(blah...., i)
end
gif(anim, path_var)

is would be natural to parallelize this process since @animate IO a bunch of temp files to disks?

after looking at code base it appears that the current approach is to construct a big expression, having for loop and counter, at every frame use frame(plt = current()) calling png() to save to a file, which means this cannot be parallel because of the dependency on current() plot also file io.

2 Likes

Indeed, this would make a good PR :slight_smile:

ok at the moment the following minimal example would crash so it’s not trivial what to do

Threads.@threads for i = 2:10
    plot(1:1:i, rand(i))
    Plots.png(Plots.current(), "$i.png")
end

crash in Plots.png)

That is also using current() this

Threads.@threads for i = 2:10
    pl = plot(1:1:i, rand(i))
    Plots.png(pl, "$i.png")
end

would not, but now that I think of it, there is a lot of mutable global state in Plots, that this is also not threadsafe.

this would also fail, because png just would crash

The png part actually worked for me

plts = [ plot(rand(i)) for i in 2:10];
Threads.@threads for i = 1:9
       Plots.png(plts[i], "plt$i.png")
end

I believe that threads do not guarantee any specific execution order, so I’m not sure what type of animation you can make other than one with random frames.

no need to guarantee execution order if plots are fixed by index i

are you actually running more than 1 threads? Threads.nthreads()

what version of Julia and Plots is this, I get GR segmentation faults.