Sat, 20 Feb 2021 16:26:31 -0500
Add "extra" string output to simple_iterate.
5 | 1 | ######################################### |
2 | # Helpers for communication via channels | |
3 | ######################################### | |
4 | ||
5 | module Comms | |
6 | ||
7 | __precompile__() | |
8 | ||
9 | ############## | |
10 | # Our exports | |
11 | ############## | |
12 | ||
13 | export process_channel, | |
6 | 14 | put_onlylatest!, |
15 | put_unless_closed! | |
5 | 16 | |
17 | #################### | |
18 | # Channel iteration | |
19 | #################### | |
20 | ||
21 | function process_channel(fn, rc) | |
22 | while true | |
23 | d=take!(rc) | |
24 | # Take only the latest image to visualise | |
25 | while isready(rc) | |
26 | d=take!(rc) | |
27 | end | |
28 | # We're done if we were fed nothing | |
29 | if isnothing(d) | |
30 | break | |
31 | end | |
32 | try | |
33 | fn(d) | |
34 | catch ex | |
6 | 35 | error("Exception in process_channel handler. Terminating.\n") |
36 | rethrow(ex) | |
5 | 37 | end |
38 | end | |
39 | end | |
40 | ||
41 | ############################################# | |
42 | # Ensure only latest data is in a Channel(1) | |
43 | ############################################# | |
44 | ||
45 | function put_onlylatest!(rc, d) | |
46 | while isready(rc) | |
47 | take!(rc) | |
48 | end | |
49 | put!(rc, d) | |
50 | end | |
51 | ||
6 | 52 | ############################################ |
53 | # Cracefully return false if channel closed | |
54 | ############################################ | |
55 | ||
56 | function put_unless_closed!(rc, d) | |
57 | try | |
58 | put!(rc, d) | |
59 | catch ex | |
60 | if isa(ex, InvalidStateException) && ex.state==:closed | |
61 | return false | |
62 | else | |
63 | rethrow(ex) | |
64 | end | |
65 | end | |
66 | return true | |
67 | end | |
68 | ||
5 | 69 | end # Module |