|
Programmer's Notebook |
All computer source code presented on this page, unless it includes attribution to another author, is provided by Ed Halley under the Artistic License. Use such code freely and without any expectation of support. I would like to know if you make anything cool with the code, or need questions answered.
python/ bindings.py boards.py buzz.py cache.py cards.py constraints.py csql.py english.py getopts.py gizmos.py goals.py improv.py interpolations.py namespaces.py nihongo.py nodes.py octalplus.py patterns.py persist.py physics.py pids.py pieces.py quizzes.py recipes.py relays.py romaji.py ropen.py sheets.py strokes.py subscriptions.py svgbuild.py testing.py things.py timing.py ucsv.py useful.py uuid.py vectors.py weighted.py java/ CSVReader.java CSVWriter.java GlobFilenameFilter.java RegexFilenameFilter.java StringBufferOutputStream.java ThreadSet.java Throttle.java TracingThread.java Utf8ConsoleTest.java droid/ ArrangeViewsTouchListener.java DownloadFileTask.java perl/ CVQM.pm Kana.pm Typo.pm cxx/ CCache.h equalish.cpp |
Download ropen.py
|
# -*- python -*- ''' Open a file with three re-tries. ABSTRACT On some networked filesystems, when dealing with tens of thousands of data files quickly, the remote system may run out of file handles or experience some other temporary problem. Generally, these problems will reset immediately, but only after a file access has failed. Rather than completely stop the process in the middle, and try to resume the process later, this code will try to hide the temporary problem. It forms a drop-in replacement for the core open() function. When in use, any call to this ropen() will transparently try to open the file three times, with a short delay between each retry. SYNOPSIS >>> from ropen import ropen as open >>> for x in xrange(100000): ... f = open( flaky_network_file, 'r' ) ... f.close() AUTHOR Ed Halley (ed@halley.cc) 10 September 2007 ''' import time import random _RANDOM = random.Random() def ropen(*args): '''Drop-in replacement for core open() function. Allows three transparent retries to mask some network problems. ''' raised = None retries = 3 while retries > 0: retries -= 1 try: return open(*args) except Exception, ex: raised = ex time.sleep(_RANDOM.random() + 0.25) raise raised |
|
Contact Ed Halley by email at
ed@halley.cc. Text, code, layout and artwork are Copyright © 1996-2013 Ed Halley. Copying in whole or in part, with author attribution, is expressly allowed. Any references to trademarks are illustrative and are controlled by their respective owners. |
|