From 4cc4fbb13a89b834c9d26adf408ac6d08e3f7603 Mon Sep 17 00:00:00 2001
From: jwikmark <jacob@wikmark.se>
Date: Wed, 14 Mar 2018 09:44:54 +0100
Subject: [PATCH] threading

---
 computerlabserver.jl | 58 +++++++++++++++++++++++++-------------------
 1 file changed, 33 insertions(+), 25 deletions(-)

diff --git a/computerlabserver.jl b/computerlabserver.jl
index 1b5c08b..20d4cf8 100644
--- a/computerlabserver.jl
+++ b/computerlabserver.jl
@@ -1,51 +1,59 @@
-using ControlSystems, InteractNext, Plots, LabGUI, Mux
+@everywhere using ControlSystems, InteractNext, Plots, LabGUI, Mux
+@everywhere inspectdr(show=false, size=(800,800))
 
-inspectdr(show=false, size=(800,800))
-setPlotScale("log10")
+@everywhere setPlotScale("log10")
 
-const Ω = logspace(-2,2,100)
+@everywhere const Ω = logspace(-2,2,100)
 
-struct session
+if length(workers())==1
+    warn("Only one worker!")
+end
+
+struct Session
     ui::WebIO.Node{WebIO.DOM}
     widgets::Array{WebIO.Scope, 1}
     defaults::Vector{Float64}
 end
 
-function zerosession(s::session)
+function zerosession(s::Session)
     for i in 1:length(s.widgets)
-        obs(s.widgets[i])[] = s.defaults[i]
+        observe(s.widgets[i])[] = s.defaults[i]
     end
 end
-    
-function session()
-    ui = InteractNext.@manipulate for ζ = linspace(0,1.5,30), ω = linspace(0.001,2,20), g = linspace(0.001,8,20)
-        G  = tf([g*ω^2], [1, 2ζ*ω, ω^2])
+   
+@everywhere function makeplot(ζ, ω, g)
+    G  = tf([g*ω^2], [1, 2ζ*ω, ω^2])
 
-        bp = bodeplot(G, Ω, l=(3,), ylims=(0.01,20), plotphase=false); 
-        vline!([ω], l=(:dash,:black), text=["w"]);
-        hline!([g], l=(:dash,:black), text=["g"])
+    bp = bodeplot(G, Ω, l=(3,), ylims=(0.01,20), plotphase=false); 
+    vline!([ω], l=(:dash,:black), text=["w"]);
+    hline!([g], l=(:dash,:black), text=["g"])
 
-        np = nyquistplot(G, Ω, l=(3,), logspace(-3,3,100), xlims = (-3,3), ylims=(-8,2))
+    np = nyquistplot(G, Ω, l=(3,), logspace(-3,3,100), xlims = (-3,3), ylims=(-8,2))
 
-        pzp = pzmap(G, xlims = (-2,0), ylims = (-√(2),√(2)))
+    pzp = pzmap(G, xlims = (-2,0), ylims = (-√(2),√(2)))
 
-        sp = stepplot(c2d(G,0.1), 20, l=(3,), ylims=(0,8))
-        hline!([g], l=(:dash,:black), text=["g"])
-        vline!([2/ω], l=(:dash,:black), text=["2/w"])
+    sp = stepplot(c2d(G,0.1), 20, l=(3,), ylims=(0,8))
+    hline!([g], l=(:dash,:black), text=["g"])
+    vline!([2/ω], l=(:dash,:black), text=["2/w"])
 
-        plot(bp,sp,pzp,np, size = (800,800))#, layout = @layout([[a c{0.6h}] b d]))
+    plot(bp,sp,pzp,np, size = (800,800))#, layout = @layout([[a c{0.6h}] b d]))
+end
+
+function Session()
+    ui = InteractNext.@manipulate for ζ = linspace(0,1.5,30), ω = linspace(0.001,2,20), g = linspace(0.001,8,20)
+        @fetch(makeplot(ζ, ω, g))
     end
     widgets = [ζ, ω, g]
-    session(ui, widgets, [obs(i)[] for i in widgets])
+    Session(ui, widgets, [observe(i)[] for i in widgets])
 end
 
 const nsessions = 1000
 global n = 1
-const sessions = session[]
+const sessions = Array{Session,1}(nsessions)
 
-@async for i in 1:nsessions
-    print("Creating session $i...\n")
-    push!(sessions, session())
+@async (sleep(1.0); print("\nSessions are being created, this may take a while\n"))
+for i in 1:nsessions
+    @async (sessions[i] = Session(); print("Created session $i\n"))
 end
 
 function responder(req)
-- 
GitLab