# HG changeset patch # User Tuomo Valkonen # Date 1576082734 -7200 # Node ID 015025cf2a50f30bb98c34c72b8135576f0b6cdb # Parent 59fd17a3cea008677db5ee660b2a400f7d91d01c Add Channel comms helper module diff -r 59fd17a3cea0 -r 015025cf2a50 src/AlgTools.jl --- a/src/AlgTools.jl Tue Nov 19 10:23:58 2019 -0500 +++ b/src/AlgTools.jl Wed Dec 11 18:45:34 2019 +0200 @@ -7,5 +7,6 @@ include("LinkedLists.jl") include("Iterate.jl") include("Util.jl") +include("Comms.jl") end diff -r 59fd17a3cea0 -r 015025cf2a50 src/Comms.jl --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/Comms.jl Wed Dec 11 18:45:34 2019 +0200 @@ -0,0 +1,51 @@ +######################################### +# Helpers for communication via channels +######################################### + +module Comms + +__precompile__() + +############## +# Our exports +############## + +export process_channel, + put_onlylatest! + +#################### +# Channel iteration +#################### + +function process_channel(fn, rc) + while true + d=take!(rc) + # Take only the latest image to visualise + while isready(rc) + d=take!(rc) + end + # We're done if we were fed nothing + if isnothing(d) + break + end + try + fn(d) + catch ex + error("Exception in process_channel handler. Terminating.\n$(ex)") + throw(ex) + end + end +end + +############################################# +# Ensure only latest data is in a Channel(1) +############################################# + +function put_onlylatest!(rc, d) + while isready(rc) + take!(rc) + end + put!(rc, d) +end + +end # Module