How to bypass a request when it takes too long?

Viewed 231

I have a python library which must be fast enough for online application. If a particular request (function call) takes too long, I want to just bypass this request with an empty result returned.

The function looks like the following:

def fast_function(text):

    result = mylibrary.process(text)
    ...

If the mylibrary.process spend time more than a threshold limit, i.e. 100 milliseconds, I want to bypass this request and proceed to process the next 'text'.

What's the normal way to handle this? Is this a normal scenario? My application can afford to bypass a very small number of requests like this, if it takes too long.

2 Answers

One way is to use a signal timer. As an example:

import signal

def took_too_long():
    raise TimeoutError

signal.signal(signal.SIGALRM, took_too_long)
signal.setitimer(signal.ITIMER_REAL, 0.1)      # 0.1 seconds
try:
    result = mylibrary.process(text)
    signal.setitimer(signal.ITIMER_REAL, 0)    # success, reset to 0 to disable the timer
except TimeoutError:
    # took too long, do something

You'll have to experiment to see if this does or does not add too much overhead.

You can add a timeout to your function.

One way to implement it is to use a timeout decorator which will throw an exception if the function runs for more than the defined timeout. In order to pass to the next operation you can catch the exception thrown by the timeout.

Install this one for example: pip install timeout-decorator

import timeout_decorator

@timeout_decorator.timeout(5) # timeout of 5 seconds
def fast_function(text):
    result = mylibrary.process(text)
Related