uWSGI, gevent, a few redis calls and how to timeout a post if greater than 90 ms

[亡魂溺海] 提交于 2019-12-23 04:36:15

问题


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"

回答1:


# 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()


来源:https://stackoverflow.com/questions/13001000/uwsgi-gevent-a-few-redis-calls-and-how-to-timeout-a-post-if-greater-than-90-ms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!