您可以在设置的超时间隔后调用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)。
Yield Server 任务结果:在 try/except 块中,当值可用时将产生结果,或者如果超时并断开连接,则会发生错误,并且将返回Request dropped 消息。
另类
这看起来确实不是一个理想的场景,应该尽可能避免,但我可以看到需要这种功能。此外,尽管很少见,但请记住 loseConnection 并不总是完全关闭连接(这是由于 TCP 实现不太扭曲)。更好的解决方案是在客户端断开连接时取消服务器端任务(这可能更容易捕获)。这可以通过将addErrback 附加到Request.notifyFinish() 来完成。这是一个仅使用 Twisted (http://twistedmatrix.com/documents/current/web/howto/web-in-60/interrupted.html) 的示例。