aboutsummaryrefslogtreecommitdiff
path: root/examples/redis-async.tcl
blob: b9ec27ac4ab08b13a4fd36d95c94cc885262c1fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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