# Animation with threads in Plots.jl?

**URL:** <https://discourse.julialang.org/t/animation-with-threads-in-plots-jl/33958>\
**Category:** General Usage\
**Tags:** plotting, multithreading, plots, animations\
**Created:** [January 30, 2020, 2:14am UTC](https://discourse.julialang.org/t/animation-with-threads-in-plots-jl/33958 "2020-01-30T02:14:34Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [January 30, 2020, 2:14am UTC](https://discourse.julialang.org/t/animation-with-threads-in-plots-jl/33958/1 "2020-01-30T02:14:34Z")

</div>

Since the canonical way of making animation involes:

```julia
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.

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [January 30, 2020, 11:51am UTC](https://discourse.julialang.org/t/animation-with-threads-in-plots-jl/33958/2 "2020-01-30T11:51:25Z")

</div>

Indeed, this would make a good PR 🙂

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [January 30, 2020, 9:45pm UTC](https://discourse.julialang.org/t/animation-with-threads-in-plots-jl/33958/3 "2020-01-30T21:45:41Z")

</div>

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

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

```

crash in `Plots.png`)

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [January 30, 2020, 10:16pm UTC](https://discourse.julialang.org/t/animation-with-threads-in-plots-jl/33958/4 "2020-01-30T22:16:25Z")

</div>

That is also using `current()` this

```julia
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.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [January 30, 2020, 10:19pm UTC](https://discourse.julialang.org/t/animation-with-threads-in-plots-jl/33958/5 "2020-01-30T22:19:43Z")

</div>

this would also fail, because png just would crash

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [January 30, 2020, 10:29pm UTC](https://discourse.julialang.org/t/animation-with-threads-in-plots-jl/33958/6 "2020-01-30T22:29:13Z")

</div>

The png part actually worked for me

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

```

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [January 30, 2020, 10:33pm UTC](https://discourse.julialang.org/t/animation-with-threads-in-plots-jl/33958/7 "2020-01-30T22:33:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [January 30, 2020, 10:35pm UTC](https://discourse.julialang.org/t/animation-with-threads-in-plots-jl/33958/8 "2020-01-30T22:35:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [January 30, 2020, 11:04pm UTC](https://discourse.julialang.org/t/animation-with-threads-in-plots-jl/33958/9 "2020-01-30T23:04:29Z")

</div>

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

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