From 7064a411c2d6b2993bf05904c9e7447d747674bc Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Mon, 25 Mar 2013 22:36:23 -0400 Subject: [PATCH] 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 --- testcases/filtertestcase.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/testcases/filtertestcase.py b/testcases/filtertestcase.py index 4aef2c9d..a20a932d 100644 --- a/testcases/filtertestcase.py +++ b/testcases/filtertestcase.py @@ -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