Chuck Esterbrook wrote:
I did this in a real project where a nightly cron job would compute
customer balances, charge their cards, send them "your card is denied"
messages, etc. Continuing to the next customer when one failed was
much better than stopping all billing. Often the failures were
confined to very specific cases and only 1 or 2 customers would fail
while the others would proceed just fine.
I have done similarly with exceptions: Here is some code I wrote a month
ago for uploading stuff to Amazon S3. It uses the BitBucket library for
talking to S3. Sometimes the upload fails by amazon just dropping the
connection or whatever and I want to retry. This is another case where
retrying is a good thing:
# This while causes us to loop which retries the
# upload. We increment attempts each time through and if
# attempts is less than 3 we raise ValueError which gets
# caught with a pass which causes us to hit the bottom of
# the loop so we start over. If attempts is 3 or greater
# we don't raise ValueError which causes us to hit the
# else clause which contains a break which gets us out of
# the loop.
attempts = 0
while 1:
# This try for implementing the retry mechanism
try:
# This try for ignoring empty files
try:
if bucket.has_key(record[0]):
bits = bucket[record[0]]
bits.filename = file
else:
bits = bitbucket.Bits(filename=file)
# Here's where we assign bits (read in
# from the file) to record[0] (the
# filename in S3) to a key in the bucket.
bucket[record[0]] = bits
os.unlink("%s/%s" % (BACKUP_DIR, record[0]))
except bitbucket.BitBucketEmptyError:
print 'sync_dir: Empty File - Ignored %s' %
fullpath
attempts = attempts + 1
if attempts < 3:
raise ValueError
except ValueError:
print "Retrying...Retry number ", attempts+1
pass # retry
else:
break # done
--
[email protected]
http://www.kernel-panic.org/cgi-bin/mailman/listinfo/kplug-lpsg