Created
November 25, 2011 08:02
-
-
Save aliang/1393029 to your computer and use it in GitHub Desktop.
monkey patch to make pymongo not throw ridiculous AutoReconnect errors
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
# Stolen from http://paste.pocoo.org/show/224441/ | |
from pymongo.cursor import Cursor | |
from pymongo.connection import Connection | |
from pymongo.errors import AutoReconnect | |
from time import sleep | |
def reconnect(f): | |
def f_retry(*args, **kwargs): | |
while True: | |
try: | |
return f(*args, **kwargs) | |
except AutoReconnect, e: | |
print('Fail to execute %s [%s]' % (f.__name__, e)) | |
sleep(0.1) | |
return f_retry | |
Cursor._Cursor__send_message = reconnect(Cursor._Cursor__send_message) | |
Connection._send_message = reconnect(Connection._send_message) | |
Connection._send_message_with_response = reconnect(Connection._send_message_with_response) | |
Connection._Connection__find_master = reconnect(Connection._Connection__find_master) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment