设置请求超时

时间:2014-08-25 15:40:10

标签: python celery

我想为请求设置时间限制,这样,如果队列关闭,客户端就不必等待很长时间才能收到连接错误。就目前而言,当我发出请求排队时,应用程序会在我获得异常之前花费大量时间。

我尝试在客户端请求中设置time_limit,soft_time_limit,timeout和soft_timeout,但它们都没有工作。

如何在请求失败之前设置请求可以等待获取响应的超时?

以下是我用来调用的代码。

task = clusterWorking.apply_async(queue=q, soft_time_limit=2, time_limit=5)
task = clusterWorking.apply_async(queue=q, timeout=1, soft_timeout=1)

这是服务器代码。

@task(name='manager.pingdaemon.clusterWorking')
def clusterWorking():
    return "up"

1 个答案:

答案 0 :(得分:0)

你可以使用get(timeout) http://celery.readthedocs.org/en/latest/reference/celery.result.html?highlight=get#celery.result.ResultSet.get

try:
    task = clusterWorking.apply_async(queue=q).get(5)
except TimeoutError:
    pass