summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorErik M. Bray <erik.bray@lri.fr>2016-09-27 15:22:39 +0200
committerErik M. Bray <erik.bray@lri.fr>2016-09-27 17:48:29 +0200
commit1be231d529a423cc03c49491c92350999086621c (patch)
tree6c43d560e13f80508c859e58124f19b13724ceab
parent401cb36b4c1ee00bd02ea0c70509bab70e34ad02 (diff)
downloadpsutil-1be231d529a423cc03c49491c92350999086621c.tar.gz
Allow wait_for_file to also wait on empty files.
Sleep between empty reads as well.
-rw-r--r--psutil/tests/__init__.py12
1 files changed, 8 insertions, 4 deletions
diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py
index 60d540d5..336ec733 100644
--- a/psutil/tests/__init__.py
+++ b/psutil/tests/__init__.py
@@ -204,7 +204,7 @@ def get_test_subprocess(cmd=None, **kwds):
cmd = [PYTHON, "-c", pyline]
sproc = subprocess.Popen(cmd, **kwds)
try:
- wait_for_file(TESTFN)
+ wait_for_file(TESTFN, empty=True)
except RuntimeError:
warn("couldn't make sure test file was actually created")
else:
@@ -375,15 +375,19 @@ def wait_for_pid(pid, timeout=GLOBAL_TIMEOUT):
return
-def wait_for_file(fname, timeout=GLOBAL_TIMEOUT, delete_file=True):
- """Wait for a file to be written on disk with some content."""
+def wait_for_file(fname, timeout=GLOBAL_TIMEOUT, empty=False,
+ delete_file=True):
+ """Wait for a file to be written on disk with some content, or until
+ the file exists if empty=True."""
stop_at = time.time() + timeout
sleep_for = 0.001
while time.time() < stop_at:
try:
with open(fname, "r") as f:
data = f.read()
- if not data:
+ if not empty and not data:
+ time.sleep(sleep_for)
+ sleep_for *= 2
continue
if delete_file:
os.remove(fname)