Created
August 25, 2012 04:19
-
-
Save chooper/3460641 to your computer and use it in GitHub Desktop.
Automatically retry failed zerorpc requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ClientWithRetry(zerorpc.Client): | |
"""Wrapper of zerorpc.Client to automatically retry failed | |
requests.""" | |
def __init__(self,retry_attempts,retry_delay,*args,**kwargs): | |
self._retry_attempts = retry_attempts | |
self._retry_delay = retry_delay | |
zerorpc.Client.__init__(self,*args,**kwargs) | |
def __call__(self,*args,**kwargs): | |
for i in xrange(self._retry_attempts): | |
attempt = (i + 1) | |
try: | |
ret_val = zerorpc.Client.__call__(self,*args,**kwargs) | |
except (zerorpc.TimeoutError, zerorpc.LostRemote): | |
if attempt == self._retry_attempts: | |
# raise exception on last attempt | |
raise | |
else: | |
# try again a little later | |
time.sleep(self._retry_delay) | |
continue | |
else: | |
return ret_val | |
raise Exception("Somehow fell out of retry loop. Should never happen.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment