David542
David542

Reputation: 110203

Why is this decorator being called so many times?

I have the following decorator:

@acceptable_methods('POST')
def deposit_funds(request, user=None):
    # do something

And here is the decorator code:

def acceptable_methods(*args):
    """
    Enforce that the necessary method has been called.

    """
    valid_methods = args
    print '11111'

    def _method_wrapper(view_function):
        print '22222'

        def _arguments_wrapper(*args, **kwargs):
            print '33333'
            if args[0].META.get('REQUEST_METHOD') not in valid_methods:
                return JsonRestResponse(None, success=False, error="This is not an acceptable method", status_code=FORBIDDEN_METHOD)
            return view_function(*args, **kwargs)

        return _arguments_wrapper

    return _method_wrapper

For some very strang reason, it seems to run 16 times on both of the outer wrappers. Here is the print statements:

[21/Nov/2014 16:26:57] "POST /api/v1.0/bonuses/grant HTTP/1.1" 500 124805
11111
22222
11111
22222
11111
22222
11111
22222
11111
22222
11111
22222
11111
22222
11111
22222
11111
22222
11111
22222
11111
22222
11111
22222
11111
22222
11111
22222
11111
22222
11111
22222
[21/Nov/2014 16:27:13] "POST /api/v1.0/bonuses/grant HTTP/1.1" 500 66364

Every print statement has the same os.pid(). Why is this occurring? Any why isn't the inner-most method being called at all?

Upvotes: 0

Views: 75

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122122

Your WSGI server created 16 child processes and Python is being run separately for each.

Upvotes: 3

Related Questions