Created
October 26, 2015 13:31
-
-
Save 72squared/37b2fbeb448f867cf07f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import rediscluster | |
import threading | |
import time | |
import random | |
startup_nodes = [ | |
{'host':'127.0.0.1', 'port': 7000}, | |
{'host':'127.0.0.1', 'port': 7001}, | |
{'host':'127.0.0.1', 'port': 7002}, | |
] | |
def test(thread_id, test_key, run_event, client): | |
while run_event.is_set(): | |
print "thread-%s: %s" % (thread_id, client.incr(test_key)) | |
for i in xrange(0, 100): | |
client.incr("%s-%s" % (test_key, i)) | |
pipe = client.pipeline(transaction=False) | |
for i in xrange(0, 100): | |
pipe.get("%s-%s" % (test_key, i)) | |
pipe.execute() | |
time.sleep(random.uniform(0, 0.1)) | |
if __name__ == "__main__": | |
threads = [] | |
redis_client = rediscluster.StrictRedisCluster(startup_nodes=startup_nodes) | |
test_key = 'test' | |
redis_client.set(test_key, 0) | |
run_event = threading.Event() | |
run_event.set() | |
for thread_id in xrange(1, 10): | |
threads.append(threading.Thread(target=test, kwargs={'thread_id': thread_id, 'test_key': test_key, 'run_event': run_event, 'client': redis_client})) | |
for t in threads: | |
t.start() | |
try: | |
while True: | |
time.sleep(.1) | |
except (KeyboardInterrupt, SystemExit): | |
run_event.clear() | |
print '\n! Received keyboard interrupt, quitting threads.\n' | |
for t in threads: | |
t.join() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment