Last active
August 29, 2015 14:02
-
-
Save andrefsp/fef44032269fb710d216 to your computer and use it in GitHub Desktop.
Race condition on pylibmc
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 argparse | |
import multiprocessing | |
import threading | |
from pylibmc.test import make_test_client | |
import random | |
NUM_THREADS = 2 | |
NUM_PROCS = 4 | |
NUM_SETS = 10000 | |
def inner_set_values(client): | |
key, val = str(random.randrange(1, 5)), str(random.randrange(1, 10)) | |
operation = random.choice(['set', 'delete']) | |
for i in range(NUM_SETS): | |
if operation in ('delete'): | |
client.delete(key) | |
else: | |
client.set(key, val) | |
def set_values(): | |
client = make_test_client(binary=True) | |
threads = [] | |
for _ in range(NUM_THREADS): | |
th = threading.Thread(target=inner_set_values, args=(client, )) | |
th.daemon = False | |
th.start() | |
threads.append(th) | |
for t in threads: | |
t.join(None) | |
client.disconnect_all() | |
def parse_arguments(): | |
parser = argparse.ArgumentParser(description='Stress test pylibmc set') | |
parser.add_argument('-p', '--procs', default=NUM_PROCS, type=int) | |
parser.add_argument('-t', '--threads', default=NUM_THREADS, type=int) | |
parser.add_argument('-s', '--sets', default=NUM_SETS, type=int) | |
return parser.parse_args() | |
def main(): | |
ns = parse_arguments() | |
global NUM_PROCS, NUM_SETS, NUM_THREADS | |
NUM_PROCS = ns.procs | |
NUM_THREADS = ns.threads | |
NUM_SETS = ns.sets | |
procs = [multiprocessing.Process(target=set_values) for _ in range(NUM_PROCS)] | |
for proc in procs: | |
proc.start() | |
for proc in procs: | |
proc.join() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Running this will create a strange error:
Basically a NotFound exception on a memcached set operation.