1

Below is my code in bottle. I am using uWSGI with the gevent loop. From the time of the request, I need to return false if the entire request is taking longer than 90 milliseconds. I dont get how to use gevent to timeout after 90ms. The blocking codes less less than 2 ms. Its the redis calls that are the issue. Under no load or little load...entire requests takes under 20ms. Under severe load, redis calls can take longer...I need to time out if longer. So, from the first redis call, timeout if taking longer than 90 ms the return. If less than 90ms, calculate the remainder. E.g. if call one takes 60 ms then I have 30 ms for call two. If call 2 is taking longer then 30 ms then timeout.

@post('/test')
def test():

    #START THE TIMER

    #SOME BLOCKING CODE

    #MAKE A REDIS CALL AND SERVICE OTHER REQUESTS
    jt = [gevent.spawn(redis_call)]
    gevent.joinall(jt)

    #SOME BLOCKING CODE

    #MAKE A REDIS CALL AND SERVICE OTHER REQUESTS
    jt = [gevent.spawn(redis_call)]
    gevent.joinall(jt)

    if total_time<.09:
        yield "passed"
     else:
        yield "failed"
Tampa
  • 75,446
  • 119
  • 278
  • 425

1 Answers1

2
# start redis_call in a background greenlet
g = gevent.spawn(redis_call)

# wait for up to 90 seconds for redis_call to complete
g.join(90)

# if g has finished, kill() is a no-op
# if g is still running, kill() will interrupt it (by raising GreenletExit in it)
# by default, kill() waits for greenlet to exit (which might never happen, 
# if redis_call caught GreenletExit and ignored it). You can also do g.kill(block=False) to
# avoid waiting for killing to complete 
g.kill()
Denis
  • 3,760
  • 25
  • 22
  • I tried this but did not work. It did not time out. jt = gevent.spawn(cookie_redis_call,guid) jt.join(.0009) jt.kill() – Tampa Oct 21 '12 at 19:24
  • make sure you monkey patch redis-py to become cooperative – Denis Oct 21 '12 at 19:54
  • Great! It worked but how to I know if there was a timeout? Now I get none. But i need to know if there was a none due to timeout rather then none do to no key.... – Tampa Oct 21 '12 at 20:11
  • what timeout? you can check if g is still running by doing "if g:" – Denis Oct 21 '12 at 20:58