-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.py
executable file
·112 lines (95 loc) · 3.33 KB
/
client.py
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python
# Script Name : redis-master-client
# Author : amit.handa
# Created : 14.11.16
# Version : 1.0
# Description : master script to fork multiple redis-clients
import os, time, threading, signal, logging
import sys
import redis
from rediscluster import StrictRedisCluster
logging.basicConfig(level=logging.DEBUG, format='[%(levelname)s] (%(threadName)-10s) %(message)s',)
def client( cid, *clients ):
chandles = []
for c in clients:
chandles.append( redis.Redis( host = c[0], port = c[1] ) )
logging.debug( 'client %d connected to redis servers' % cid )
count = 0
while True:
time.sleep( 1 )
count+=1
print 'client: %d, servers value are ' % (cid,),
for i in range( len( chandles ) ):
chandle = chandles[i]
if i == 0:
chandle.set( cid, count )
v = chandle.get( cid )
print v,
print "end"
def threadedClient( cid, clients ):
chandles = []
for c in clients:
chandles.append( redis.Redis( host = c[0], port = c[1] ) )
logging.debug( 'client %d connected to redis servers' % cid )
count = 0
while True:
time.sleep( 1 )
count+=1
print 'client: %d, servers value are ' % (cid,),
for i in range( len( chandles ) ):
chandle = chandles[i]
if i == 0:
chandle.set( cid, count )
v = chandle.get( cid )
print v,
print "end"
def threadedClientWithCluster( cid, clients ):
hclients = [ { 'host' : c[0], 'port' : c[1] } for c in clients ]
credis= StrictRedisCluster( startup_nodes=hclients )
logging.debug( 'client %d connected to redis servers %s' % (cid, hclients) )
count = 0
while True:
time.sleep( 1 )
count+=1
print 'client: %d, servers value are ' % (cid,),
credis.set( cid, count )
v = credis.get( cid )
print v,
print "end"
def main():
logging.debug( "Hello %s %s" % ( sys.argv[1], sys.argv[2] ) )
os.setpgrp() # create new proc group, become its leader
try:
if len( sys.argv ) < 3:
print """
program <numClients> <aliveSecs> <comma-sep-redis-servers>
program 2 30 ip1:9903,ip2:9904
"""
os._exit( 1 )
redisServers = parseConfig( sys.argv[3] )
#print "parsed servers ", redisServers
"""
for i in range( int( sys.argv[1] ) ):
pid = os.fork()
if pid == 0:
client( i, *redisServers )
else:
#pids = (os.getpid(), pid)
print "parent: child: %s" % pid
"""
for i in range( int( sys.argv[1] ) ):
newthread = threading.Thread( target=threadedClientWithCluster, args=(i,redisServers) )
newthread.start()
logging.debug( "KILL after %s seconds !!!!!" % sys.argv[2] )
time.sleep( int( sys.argv[2] ) )
except:
logging.error( "ERRRRROR ! %s" % sys.exc-info()[0] )
finally:
logging.debug( "KILLLLLLLLLLING" )
os.killpg( 0, signal.SIGKILL ) # kill all procs in my group
def parseConfig( csvServers ):
# type: (str) -> list
pcsvServers = csvServers.split( ',' )
return [ csvserver.split(':') for csvserver in pcsvServers ]
if __name__ == '__main__':
main()