On 14/02/2015, at 11:42 PM, Paul Royik <[email protected]> wrote:
> Why do we need to decrement counter (and use counter at all)? To handle the fact that your call is recursive. The first call of the function for that thread the counter will be zero. When it calls itself against recursive it will be 1, so it will not reset the end time. Each time it calls recursively again the counter will keep incrementing. As it returns from each recursive call it will decrement the counter. When it finally returns from the initial call it will be back at zero. The next time it is called in that thread, it would then have to be a fresh call of the algorithm and so we start over and set end time again. > Is the following approach bad? Yes. Because the time will only be set for the first time the algorithm is run in a specific thread. Threads are reused across requests, so the end time has to be reset on a subsequent request. > def time_limit(seconds): > def decorator(func): > func.info = threading.local() > > def check_timeout(): > if time.time() > func.info.end_time: > raise TimeoutException > > func.check_timeout = check_timeout > > @functools.wraps(func) > def wrapper(*args, **kwargs): > if not hasattr(func.info, 'end_time'): > func.info.end_time = time.time() + seconds > return func(*args, **kwargs) > > return wrapper > > return decorator -- You received this message because you are subscribed to the Google Groups "modwsgi" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/modwsgi. For more options, visit https://groups.google.com/d/optout.
