Skip to content

Instantly share code, notes, and snippets.

@jvns
Last active November 26, 2015 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jvns/ce757ed05cfbdc7c47c0 to your computer and use it in GitHub Desktop.
Save jvns/ce757ed05cfbdc7c47c0 to your computer and use it in GitHub Desktop.
import random
import gevent
from collections import defaultdict
class ReadError(Exception):
pass
class Connection(object):
def __init__(self):
pass
def cleanup(self):
self.broken_state = False
def read(self):
# changes somes internal state
self.broken_state = True
# a request takes a while to com back
time_to_wait = random.randrange(990, 1100) / 100000.0
gevent.sleep(time_to_wait)
# clean everything up!
self.cleanup()
# imagine that this sometimes raises ReadError
# what could go wrong?
def safe_read(cxn):
try:
cxn.read()
except ReadError:
# We are careful! We know that `read()` can raise ReadError
# so we catch that and clean up if it happens
# you'll notice that this is what the Redis client library does -- it only catches exceptions
# that the code it's calling can raise: https://github.com/redis/redis-rb/blob/master/lib/redis/client.rb#L238
cxn.cleanup()
broken_results = defaultdict(int)
for _ in xrange(100):
cxn = Connection()
with gevent.Timeout(0.01):
try:
safe_read(cxn)
except:
# if there's a timeout, keep going -- everything should be fine right?
pass
broken_results[cxn.broken_state] += 1
print broken_results
# defaultdict(<type 'int'>, {False: 28, True: 72})
# oh no everything is broken
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment