Example #1
0
def as_new_thread(function):
    """
    A function decorator that causes the decorated function to start in a new
    thread when invoked. When the function is called, it appears to return
    immediately. Behind the scenes, the function is invoked in a newly-
    created thread with the specified arguments. The function's return value
    is ignored by the thread.
    """
    def new_function(*args, **kwargs):
        thread.start_new_thread(function, args, kwargs)
    coreutils.redocument(function, new_function)
    return new_function
Example #2
0
def when_user_tracked(function):
    """
    A decorator that can be used to decorate methods on the User class. It
    checks to see if the user object the method is being invoked on is tracked
    before allowing the method to be called. If the user is not currently being
    tracked, errors.UserNotTracked is raised. Otherwise, the function
    invocation is allowed to continue.
    """
    def new_function(user, *args, **kwargs):
        if user.tracked != User.TRACKED:
            raise errors.UserNotTracked()
        return function(*args, **kwargs)
    redocument(function, new_function)
    return new_function