annotate python/example/retry.py @ 883:3b7f67021877

python3
author Jeff Hammel <k0scist@gmail.com>
date Wed, 05 Aug 2020 09:27:14 -0700
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 :(")