ENH: _copy_lines_between_files -- read all needed, and only then write/flush at once

I think this is the one which should resolve
https://github.com/fail2ban/fail2ban/issues/103
pull/154/head
Yaroslav Halchenko 2013-03-25 22:36:23 -04:00
parent ba042c6e3a
commit 7064a411c2
1 changed files with 8 additions and 5 deletions

View File

@ -105,20 +105,23 @@ def _copy_lines_between_files(fin, fout, n=None, skip=0, mode='a', terminal_line
time.sleep(1)
if isinstance(fin, str): # pragma: no branch - only used with str in test cases
fin = open(fin, 'r')
if isinstance(fout, str):
fout = open(fout, mode)
# Skip
for i in xrange(skip):
_ = fin.readline()
# Read/Write
# Read
i = 0
lines = []
while n is None or i < n:
l = fin.readline()
if terminal_line is not None and l == terminal_line:
break
fout.write(l)
fout.flush()
lines.append(l)
i += 1
# Write: all at once and flush
if isinstance(fout, str):
fout = open(fout, mode)
fout.write('\n'.join(lines))
fout.flush()
# to give other threads possibly some time to crunch
time.sleep(0.1)
return fout