[Twisted-web] Asynchronous responses example from documentation -- why import time??

Jason Harrison drjasonharrison at gmail.com
Wed Aug 8 09:48:35 MDT 2018


Hi,

I am learning twisted-web by attempting each of the examples in the
documentation and making any (small) changes necessary for Python 3.
However when I got to the Aysnchronous Responses example I was not able to
get it to work. The example is at:

https://twistedmatrix.com/documents/current/web/howto/web-in-60/asynchronous.html

I have adapted it to be a self-contained "server" rather than a .rpy
snippet but the response was always a 404.  After much head scratching and
comparison to other examples, I discovered that by adding "import time" to
my "server" it started working.

Why is "import time" required. What is happening without the "import time"?

-Jason

Cell: 604 644 8611
Email: drjasonharrison at gmail.com
LinkedIn: http://www.linkedin.com/in/drjasonharrison
Twitter: http://twitter.com/drjasonharrison



#!/usr/bin/env python3
#
https://twistedmatrix.com/documents/current/web/howto/web-in-60/asynchronous.html
# Asynchronous responses,
#
# try
# - http://localhost:8888

import sys
from twisted.web.server import Site
from twisted.web.resource import Resource
from twisted.internet import reactor, endpoints
from twisted.web.resource import NoResource
from twisted.web.server import NOT_DONE_YET
from twisted.python import log
import time #???

log.startLogging(sys.stdout)
observer = log.PythonLoggingObserver()
observer.start()

class DelayedResource(Resource):
    def _delayedRender(self, request):
        log.msg("_delayedRender request {request!r}", request = request)
        request.write(b"<html><body>Sorry to keep you
waiting.</body></html>")
        request.finish()

    def render_GET(self, request):
        log.msg("get request {request!r}", request = request)
        reactor.callLater(5, self._delayedRender, request)
        return NOT_DONE_YET

resource = DelayedResource()
factory = Site(resource)
endpoint = endpoints.TCP4ServerEndpoint(reactor, 8888)
endpoint.listen(factory)
log.msg("running reactor")
reactor.run()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-web/attachments/20180808/0465e62c/attachment.html>


More information about the Twisted-web mailing list