put_unless_closed!

Wed, 18 Dec 2019 21:32:27 +0200

author
Tuomo Valkonen <tuomov@iki.fi>
date
Wed, 18 Dec 2019 21:32:27 +0200
changeset 6
a5c1eb932d19
parent 5
015025cf2a50
child 7
900a7e18ca01

put_unless_closed!

src/Comms.jl file | annotate | diff | comparison | revisions
--- a/src/Comms.jl	Wed Dec 11 18:45:34 2019 +0200
+++ b/src/Comms.jl	Wed Dec 18 21:32:27 2019 +0200
@@ -11,7 +11,8 @@
 ##############
 
 export process_channel,
-       put_onlylatest!
+       put_onlylatest!,
+       put_unless_closed!
 
 ####################
 # Channel iteration
@@ -31,8 +32,8 @@
         try
             fn(d)
         catch ex
-            error("Exception in process_channel handler. Terminating.\n$(ex)")
-            throw(ex)
+            error("Exception in process_channel handler. Terminating.\n")
+            rethrow(ex)
         end 
     end
 end
@@ -48,4 +49,21 @@
     put!(rc, d)
 end
 
+############################################
+# Cracefully return false if channel closed
+############################################
+
+function put_unless_closed!(rc, d)
+    try
+        put!(rc, d)
+    catch ex
+        if isa(ex, InvalidStateException) && ex.state==:closed
+            return false
+        else
+            rethrow(ex)
+        end
+    end
+    return true
+end
+
 end # Module

mercurial