aboutsummaryrefslogtreecommitdiff
path: root/examples/redis-async.tcl
diff options
context:
space:
mode:
authorSteve Bennett <steveb@workware.net.au>2023-02-06 15:55:56 +1000
committerSteve Bennett <steveb@workware.net.au>2023-02-13 10:44:10 +1000
commitaa18a0d938ca171fcf96616cb7ff011034eb5902 (patch)
tree77861adb8abf52a18b23420b9f813995b71ed850 /examples/redis-async.tcl
parenta5ea6b096e9e9f9913ad860a847e3757580dd9e4 (diff)
downloadjimtcl-aa18a0d938ca171fcf96616cb7ff011034eb5902.zip
jimtcl-aa18a0d938ca171fcf96616cb7ff011034eb5902.tar.gz
jimtcl-aa18a0d938ca171fcf96616cb7ff011034eb5902.tar.bz2
redis: Add -async support
Supports communication with redis as part of an event loop Signed-off-by: Steve Bennett <steveb@workware.net.au>
Diffstat (limited to 'examples/redis-async.tcl')
-rw-r--r--examples/redis-async.tcl62
1 files changed, 62 insertions, 0 deletions
diff --git a/examples/redis-async.tcl b/examples/redis-async.tcl
new file mode 100644
index 0000000..b9ec27a
--- /dev/null
+++ b/examples/redis-async.tcl
@@ -0,0 +1,62 @@
+#!/usr/bin/env jimsh
+
+# Testing redis client access in non-blocking mode
+
+# Requires the redis extension
+package require redis
+
+# A redis server should be running either on localhost 6379
+# or on the given address (e.g. host:port)
+try {
+ lassign $argv addr
+ if {$addr eq ""} {
+ set addr localhost:6379
+ }
+ set s [socket stream $addr]
+ # socket must be in non-blocking mode
+ $s ndelay 1
+ set r [redis -async $s]
+} on error msg {
+ puts [errorInfo $msg]
+ exit 1
+}
+
+# List of outstanding redis commands
+set cmds {}
+
+$r readable {
+ while {1} {
+ set result [$r -type read]
+ if {$result eq ""} {
+ break
+ }
+ set cmds [lassign $cmds cmd]
+ # Show command and response
+ puts "$cmd => $result"
+ }
+}
+
+# queue a command and remember it
+proc redis_command {r args} {
+ global cmds
+ lappend cmds $args
+ $r {*}$args
+}
+
+redis_command $r SET zz 0
+
+proc periodic {r} {
+ global counter done
+
+ if {[incr counter] > 10} {
+ incr done
+ } else {
+ redis_command $r INCR zz
+ after 100 periodic $r
+ }
+}
+
+set counter 0
+periodic $r
+
+vwait done