
Untitled
By: a guest on
Aug 10th, 2012 | syntax:
None | size: 1.24 KB | hits: 8 | expires: Never
How to make failover in python?
again = 1
start = 1
try:
def countthis():
for i in range (start,200):
again = i
print i
except:
print "Failure occured, I will try again"
start = again
countthis.run()
def countthis(start=0, end=100):
for i in range(start, end):
print i
if i == 5:
raise Exception('5 failed')
yield i
ret = 0
end = 100
while ret < end - 1:
try:
for i in countthis(start=ret, end=end):
ret = i
except Exception, ex:
print ex
# when 5 reached, an exception will be raised, so here we restart at '6'
ret = ret + 2
0
1
2
3
4
5
5 failed
6
7
......
99
def f():
print "f called: ",
import random
x = random.randint(0, 10) / 8
print "1/x =", 1/x
while True:
try:
f()
break
except ZeroDivisionError:
print "f failed"
continue
def f():
import random
for i in range(1, 10):
while True:
try:
print i,
# do something with i:
print 1 / (i // random.randint(0, 8))
break
except ZeroDivisionError:
continue
f()