annotate python/example/retry.py @ 771:c1f314c518c9

more characters
author Jeff Hammel <k0scist@gmail.com>
date Thu, 10 Mar 2016 13:13:14 -0800
parents 88f19ebb43ea
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
758
88f19ebb43ea add example retry function
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 def retry(f, retries=5, args=(), kw=None, exceptions=()):
88f19ebb43ea add example retry function
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2
88f19ebb43ea add example retry function
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3 kw = kw or {}
88f19ebb43ea add example retry function
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 for index in range(retries):
88f19ebb43ea add example retry function
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 try:
88f19ebb43ea add example retry function
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6 return f(*args, **kw)
88f19ebb43ea add example retry function
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 except Exception as e:
88f19ebb43ea add example retry function
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 if isinstance(e, exceptions):
88f19ebb43ea add example retry function
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 print ("something bad happen")
88f19ebb43ea add example retry function
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 else:
88f19ebb43ea add example retry function
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 raise
88f19ebb43ea add example retry function
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 raise RetryTimeout("Tries a bunch of times :(")