A Simple Decorator for Simulating Delays in Method Calls
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
from functools import wraps | |
from random import randint | |
# Example Usage: | |
# | |
# @random_sleep | |
# def get_users(request): | |
# return database.users() | |
# | |
def random_sleep(f): | |
@wraps(f) | |
def wrapper(*args, **kwargs): | |
seconds = randint(1, 3) #Inclusive, change these boundaries as required | |
print 'Sleeping for ' + str(seconds) + ' seconds...' | |
time.sleep(seconds) | |
return f(*args, **kwargs) | |
return wrapper |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment