# How to parallelize computation and plotting?

**URL:** <https://discourse.julialang.org/t/how-to-parallelize-computation-and-plotting/24746>\
**Category:** Visualization\
**Tags:** plotting\
**Created:** [May 29, 2019, 8:29pm UTC](https://discourse.julialang.org/t/how-to-parallelize-computation-and-plotting/24746 "2019-05-29T20:29:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![cserteGT3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/csertegt3/32/8283_2.png) [@cserteGT3](https://discourse.julialang.org/u/cserteGT3)\
**Post date:** [May 29, 2019, 8:29pm UTC](https://discourse.julialang.org/t/how-to-parallelize-computation-and-plotting/24746/1 "2019-05-29T20:29:36Z")

</div>

The title is a bit misleading, because I would like to get a sensor’s data and plot it immadiately (so not a result of simulation or something). I get the values via TCP connection (which I can setup and print the values to console), but I want to construct a simple plot with the latest values (probably with Makie). My problems: 1) the sampling frequency is relatively high (~100 Hz) and 2) there are Makie examples only for “limited datasets”, where one iterates through the values in a range (and none where the data comes from “external source”).  
(Of course) I don’t expect a fully fledged solution, I am interested in concepts or examples (which I might have overlooked).

(I asked/proposed this question [here](https://discourse.julialang.org/t/real-time-plotting-with-makie/20432), but this time reformulated it to be more general.)

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [May 29, 2019, 9:14pm UTC](https://discourse.julialang.org/t/how-to-parallelize-computation-and-plotting/24746/2 "2019-05-29T21:14:44Z")

</div>

Just from a general computing perspective, you aren’t going to plot at 100Hz, it seems most likely you’d plot maybe 1 to 4 times per second, using a buffer to accumulate the data.

I think you could probably do this just fine with Julia Tasks, basically one task reads data from a queue and plots it, the other task reads data from the TCP connection and puts it into the queue for plotting…

---

<div class="post-metadata">

**Author:** ![asinghvi17](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asinghvi17/32/8272_2.png) [@asinghvi17](https://discourse.julialang.org/u/asinghvi17)\
**Post date:** [May 30, 2019, 6:03am UTC](https://discourse.julialang.org/t/how-to-parallelize-computation-and-plotting/24746/3 "2019-05-30T06:03:38Z")

</div>

In terms of pushing data to a plot, it’s pretty easy in `Makie` - there are a couple of unpublished examples, but it ultimately boils down to pushing to an Observable array of `Point`s.

---

<div class="post-metadata">

**Author:** ![cserteGT3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/csertegt3/32/8283_2.png) [@cserteGT3](https://discourse.julialang.org/u/cserteGT3)\
**Post date:** [June 3, 2019, 11:29am UTC](https://discourse.julialang.org/t/how-to-parallelize-computation-and-plotting/24746/4 "2019-06-03T11:29:10Z")

</div>

Thank you for the suggestions, I posted an MWE at the older post ([link to repo](https://github.com/cserteGT3/RealtimeplotExample/)).

I used tasks and I can update the plot 4 times a second (but not yet tested for larger number of updates).  
I am just curious if my approach is correct.

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [June 3, 2019, 11:48am UTC](https://discourse.julialang.org/t/how-to-parallelize-computation-and-plotting/24746/5 "2019-06-03T11:48:57Z")

</div>

Based on the manual, it seems like using a Channel object would be the usual method for sending data between tasks, rather than the heavier weight TCP socket route.

---

<div class="post-metadata">

**Author:** ![cserteGT3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/csertegt3/32/8283_2.png) [@cserteGT3](https://discourse.julialang.org/u/cserteGT3)\
**Post date:** [June 3, 2019, 11:57am UTC](https://discourse.julialang.org/t/how-to-parallelize-computation-and-plotting/24746/6 "2019-06-03T11:57:51Z")

</div>

The value’s source is another PC, therefore the TCP. The sender side is only implemented here to give a full working example.

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [June 3, 2019, 1:05pm UTC](https://discourse.julialang.org/t/how-to-parallelize-computation-and-plotting/24746/7 "2019-06-03T13:05:15Z")

</div>

I see, you’re using low level thread sync primitives for this. It seems to me a Channel is a higher level abstraction that’s probably easier to use and more straightforward to understand. Basically you’d have two tasks, one does (in pseudocode)

```julia
while(true)
   read from socket
   send to channel
end

```

and the other does

```julia
while(true)
   read from channel
   plot
end

```

---

<div class="post-metadata">

**Author:** ![cserteGT3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/csertegt3/32/8283_2.png) [@cserteGT3](https://discourse.julialang.org/u/cserteGT3)\
**Post date:** [June 3, 2019, 1:11pm UTC](https://discourse.julialang.org/t/how-to-parallelize-computation-and-plotting/24746/8 "2019-06-03T13:11:58Z")

</div>

Oh I see. I did not considered using two threads (yet) as the plotting thread is the “main julia thread”.  
I’ll consider this approach, thank you for the suggestion!
