Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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