Asynchronous responses example from documentation -- why import time??

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... 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@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... # 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()

On Wed, Aug 8, 2018 at 11:51 AM Jason Harrison <drjasonharrison@gmail.com> wrote:
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...
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"?
One possible explanation is that it has nothing to do with import time per se. Instead, *any* change to your source file would have fixed the problem. This could be because you had "stale" pyc files lying around (cached bytecode the interpreter *thought* was up-to-date with your source but was actually outdated). It could also be that your server process was left running and was still using your old code. Then, for some reason, after you added the import time your server got the new version of the code (perhaps you're relying on an auto-reloader and it missed a change, for example, or you just forgot to restart the server yourself). import time itself is definitely not a requirement for arbitrary Twisted Web-based programs to return a response other than 404. Jean-Paul
-Jason
Cell: 604 644 8611 Email: drjasonharrison@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... # 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()
_______________________________________________ Twisted-web mailing list Twisted-web@twistedmatrix.com https://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web
participants (2)
-
Jason Harrison
-
Jean-Paul Calderone