【问题标题】:How to set the server timeout in python Klein?如何在 python Klein 中设置服务器超时?
【发布时间】:2016-09-20 15:04:09
【问题描述】:

我正在使用 python Klein http://klein.readthedocs.io/en/latest/ 来设置 Web 服务。我检查了文档,但我仍然不知道如何设置服务的超时。任何更熟悉工具的人都可以展示如何将超时设置为 15 秒吗?谢谢!

【问题讨论】:

  • 你想要什么超时?会话,请求,......?
  • 我相信请求超时?所以当服务端收到一个调用并且在固定的时间范围内(可能是10秒)无法响应时,它会向客户端返回超时。
  • 好的。下次有klien 问题时,您可以将twisted 添加到标签中吗?这样扭曲的开发者也可以找到你的问题。

标签: python web-services klein-mvc


【解决方案1】:

您可以在设置的超时间隔后调用Request.loseConnection() 以断开与客户端的请求连接。这是一个简单的例子:

from twisted.internet import reactor, task, defer
from klein import Klein

app = Klein()
request_timeout = 10 # seconds

@app.route('/delayed/<int:n>')
@defer.inlineCallbacks
def timeoutRequest(request, n):
    work = serverTask(n)       # work that might take too long

    drop = reactor.callLater(
        request_timeout,    # drop request connection after n seconds
        dropRequest,        # function to drop request connection
            request,        # pass request obj into dropRequest()
            work)           # pass worker deferred obj to dropRequest()

    try:
        result = yield work     # work has completed, get result
        drop.cancel()           # cancel the task to drop the request connection
    except:
        result = 'Request dropped'

    defer.returnValue(result)

def serverTask(n):
    """
    A simulation of a task that takes n number of seconds to complete.
    """
    d = task.deferLater(reactor, n, lambda: 'delayed for %d seconds' % (n))
    return d

def dropRequest(request, deferred):
    """
    Drop the request connection and cancel any deferreds
    """
    request.loseConnection()
    deferred.cancel()

app.run('localhost', 9000)

要尝试此操作,请转至http://localhost:9000/delayed/2,然后转至http://localhost:9000/delayed/20,以测试任务未按时完成的场景。不要忘记取消与此请求相关的所有任务、延迟、线程等,否则您可能会浪费大量内存。

代码说明

服务器端任务:客户端以指定的延迟值前往/delayed/&lt;n&gt; 端点。服务器端任务 (serverTask()) 启动,为了简单起见并模拟繁忙的任务,deferLater 用于在 n 秒后返回一个字符串。

请求超时:使用callLater函数,在request_timeout间隔之后,调用dropRequest函数并传递request和所有需要取消的工作延迟(在这种情况下)只有work)。当request_timeout 已通过时,请求连接将关闭(request.loseConnection())并取消延迟(deferred.cancel)。

Yield Server 任务结果:在 try/except 块中,当值可用时将产生结果,或者如果超时并断开连接,则会发生错误,并且将返回Request dropped 消息。

另类

这看起来确实不是一个理想的场景,应该尽可能避免,但我可以看到需要这种功能。此外,尽管很少见,但请记住 loseConnection 并不总是完全关闭连接(这是由于 TCP 实现不太扭曲)。更好的解决方案是在客户端断开连接时取消服务器端任务(这可能更容易捕获)。这可以通过将addErrback 附加到Request.notifyFinish() 来完成。这是一个仅使用 Twisted (http://twistedmatrix.com/documents/current/web/howto/web-in-60/interrupted.html) 的示例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-27
    • 1970-01-01
    • 1970-01-01
    • 2012-09-21
    • 2017-05-18
    • 2013-01-14
    • 2010-11-23
    • 2022-01-21
    相关资源
    最近更新 更多