如何在python Klein中设置服务器超时?

2024-06-16 11:38:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用pythonkleinhttp://klein.readthedocs.io/en/latest/来设置一个web服务。我已经检查了文档,但是我仍然不知道如何设置服务超时。更熟悉这个工具的人能演示如何将超时设置为15秒吗?谢谢!在


Tags: 工具文档iowebreadthedocslatestenklein
1条回答
网友
1楼 · 发布于 2024-06-16 11:38:22

您可以调用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/<n>端点。服务器端任务(serverTask())启动,为了简单起见并模拟繁忙的任务,deferLater被用来在n秒后返回一个字符串。在

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

生成服务器任务结果:在try/except块中,当该值可用时将生成结果,或者,如果超时时间已过并断开连接,则将发生错误并返回Request dropped消息。在

替代方案

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

相关问题 更多 >