uWSGI,gevent,一些redis调用以及如果超过90毫秒则如何使帖子超时 [英] uWSGI, gevent, a few redis calls and how to timeout a post if greater than 90 ms

查看:223
本文介绍了uWSGI,gevent,一些redis调用以及如果超过90毫秒则如何使帖子超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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"

推荐答案

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

这篇关于uWSGI,gevent,一些redis调用以及如果超过90毫秒则如何使帖子超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆