Python Lover(8)Twisted Basic - async - logging

Python Lover(8)Twisted Basic - async - logging

1. Asynchronous Response
http://twistedmatrix.com/documents/current/web/howto/web-in-60/asynchronous.html

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>
        """)

    def render_GET(self, request):
        reactor.callLater(5, self._deplayedRender, request) # delay 5 seconds
        return NOT_DONE_YET

resource = DeplayedResource()

Command to start
> twistd -n web --path .

Visit this page to verify
http://localhost:8080/RestAPI.rpy

2. Deferred in AsyncResponse
http://twistedmatrix.com/documents/current/web/howto/web-in-60/asynchronous-deferred.html

Deferred provides callbacks and errbacks.
Deferred.addCallback  Deferred.callback
Deferred.addErrback    Deferred.errback

from twisted.internet.task import deferLater

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()

    def render_GET(self, request):
        d = deferLater(reactor, 5, lambda : request)
        d.addCallback(self._delayedRender)
        return NOT_DONE_YET

resource = DelayedResource()

3. Interrupted Response
http://twistedmatrix.com/documents/current/web/howto/web-in-60/interrupted.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()

    def _responseFailed(self,err,call):
        call.cancel()

    def render_GET(self,request):
        call = reactor.callLater(5, self._delayedRender, request)
        request.notifyFinish().addErrback(self._responseFailed, call)
        return NOT_DONE_YET

resource = DelayedResource()

4. Logging Error
http://twistedmatrix.com/documents/current/web/howto/web-in-60/logging-errors.html

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()

    def _responseFailed(self, failure, call):
        call.cancel()
        err(failure,"Async response demo interrupted response")

    def render_GET(self, request):
        call = reactor.callLater(5, self._delayedRender, request)
        request.notifyFinish().addErrback(self._responseFailed, call)
        return NOT_DONE_YET

resource = DelayedResource()

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.


5. Access Logging
http://twistedmatrix.com/documents/current/web/howto/web-in-60/access-logging.html

We can configure the logging in 2 different ways.
factory = Site(root, logPath=b”/tmp/access-logging-demo.log”)

or

>twistd -n web - - logfile /tmp/access-logging-demo.log

> twistd -n web --path . --logfile ./RestAPI.access.log

References:
http://twistedmatrix.com/documents/15.0.0/core/howto/index.html
http://twistedmatrix.com/documents/current/web/howto/web-in-60/

deployment
http://twistedmatrix.com/trac/wiki/TwistedWeb
https://github.com/todddeluca/daemoncmd

https://bitbucket.org/tildeslash/monit/

你可能感兴趣的:(Python Lover(8)Twisted Basic - async - logging)