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)
您可以调用
Request.loseConnection()
在设置的超时间隔后断开与客户机的请求连接。下面是一个简单的例子:要尝试这个方法,请转到
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)的示例。在相关问题 更多 >
编程相关推荐