Basic Differences
We do not directly response string, we call Request.write. We can call request.write many times, after the entire response body has been passed to Request.write, the application must call Request.finish.
from twisted.web.resource import Resource
from twisted.web.server import NOT_DONE_YET
from twisted.internet import reactor
class DeplayedResource(Resource):
def _deplayedRender(self, request):
request.write("""
<html>
<body>
Sorry to keep you waiting.
</body>
</html>
""")
Since we do async, sometimes, we have not finished generate the result, but the client connections are close, so we need to abandon the response generation entirely.
Cancel the Generation Process
from twisted.web.resource import Resource
from twisted.web.server import NOT_DONE_YET
from twisted.internet import reactor
class DelayedResource(Resource):
def _delayedRender(self,request):
request.write("""
<html>
<body>
Sorry to keep you waiting.
</body>
</html>
""")
request.finish()
from twisted.web.resource import Resource
from twisted.web.server import NOT_DONE_YET
from twisted.internet import reactor
from twisted.python.log import err
class DelayedResource(Resource):
def _delayedRender(self, request):
request.write("""
<html>
<body>
Sorry to keep you waiting.
</body>
</html>
""")
request.finish()
These message will display when we cancel the client request.
2015-09-16 14:00:36-0500 [HTTPChannel,0,127.0.0.1] Async response demo interrupted response
Traceback (most recent call last):
Failure: twisted.internet.error.ConnectionDone: Connection was closed cleanly.