uWSGI,gevent,一些redis调用以及如果超过90 ms,如何超时

时间:2012-10-21 18:50:18

标签: python uwsgi gevent

下面是我在瓶子里的代码。我正在使用uWSGI和gevent循环。从请求的时间开始,如果整个请求的时间超过90毫秒,我需要返回false。 我不知道如何使用gevent在90ms后超时。阻塞码少于2毫秒。它的redis调用就是问题所在。无负载或小负载......整个请求不到20ms。在严重负载下,redis呼叫可能需要更长时间......如果时间更长,我需要超时。因此,从第一次redis调用开始,如果超过90 ms则返回超时。如果小于90ms,则计算余数。例如。如果呼叫一个需要60毫秒,那么我有30毫秒的呼叫二。如果呼叫2花费的时间超过30毫秒,则超时。

@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 个答案:

答案 0 :(得分: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()