annotate python/example/retry.py @ 820:737b9eda7f77

[git submodules] logging stub
author Jeff Hammel <k0scist@gmail.com>
date Tue, 31 Jan 2017 14:31:03 -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 :(")