Last active
December 21, 2015 15:09
-
-
Save drsnyder/6324489 to your computer and use it in GitHub Desktop.
barman.lockfile test
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 os | |
import multiprocessing | |
import time | |
import lockfile | |
STRING = "A" * 10 | |
def parse(line): | |
a, b, s = line.split() | |
s = s.strip() | |
a = int(a) | |
b = int(b) | |
assert a + b > 0 and s == STRING, "Error, failed to parse line %s" % line | |
return a, b, s | |
def test(file, mode, n, threadnum): | |
with lockfile.lockfile("%s.lock" % file) as locked: | |
if locked: | |
print ">> %d locked %s" % (threadnum, file) | |
fd = open(file, mode) | |
for i in range(1, n+1): | |
fd.write("%d %d %s\n" % (threadnum, i, "A" * 10)) | |
fd.flush() | |
fd.close() | |
print "%d Done" % threadnum | |
return True | |
if __name__ == '__main__': | |
NTHREADS = 80 | |
FILE = "/tmp/x.data" | |
ts = [multiprocessing.Process(target=test, args=(FILE, "a", 1000, n)) for n in xrange(NTHREADS)] | |
for t in ts: t.start() | |
for t in ts: t.join() | |
print "Ok, parsing..." | |
line_count = 0 | |
for line in open(FILE, "r"): | |
line_count += 1 | |
try: | |
parse(line) | |
except ValueError as e: | |
print "Could not parse '%s' on line %d" % (line, line_count) | |
raise e | |
print "Success! parsed %d lines" % line_count | |
os.unlink(FILE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment