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 -*- ''' 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 (email@example.com) 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
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.