Created
July 6, 2017 20:44
-
-
Save rectalogic/6d8c6c385ab39bc5b0afead8f4506125 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Model(AttrDict): | |
| read_preference = read_preferences.PRIMARY | |
| ... | |
| @contextmanager | |
| def read_preference(pref): | |
| try: | |
| prev_pref = Model.read_preference | |
| Model.read_preference = pref | |
| yield | |
| finally: | |
| Model.read_preference = prev_pref | |
| with read_preference(read_preferences.SECONDARY_PREFERRED): | |
| User.collection.find() # secondary | |
| with read_preference(read_preferences.PRIMARY): | |
| User.collection.find() # primary | |
| User.collection.find() # secondary |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment