1

下面是我在瓶子里的代码。我正在使用带有 gevent 循环的 uWSGI。从请求的时间开始,如果整个请求花费的时间超过 90 毫秒,我需要返回 false。我不知道如何在 90 毫秒后使用 gevent 超时。阻塞码小于 2 ms。它的redis调用是问题所在。在无负载或小负载下……整个请求不到 20 毫秒。在重负载下,redis 调用可能需要更长的时间……如果更长,我需要超时。因此,从第一次 redis 调用开始,如果返回时间超过 90 毫秒,则超时。如果小于 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"
4

1 回答 1

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()
于 2012-10-21T19:06:07.523 回答