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

Jason Harrison <[email protected]> Wed, 8 Aug 2018 08:48:35 -0700
Newsgroups gmane.comp.python.twisted.web
Message-ID <CAHVLaBG_fE65Vb-b1Mt9Ew9AR3TiSHTDdkQw3S8Wgka8poY1QA@mail.gmail.com>
--===============1203792383131536811==
Content-Type: multipart/alternative; boundary="0000000000008987ae0572ee7181"

--0000000000008987ae0572ee7181
Content-Type: text/plain; charset="UTF-8"

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: [email protected]
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()

--0000000000008987ae0572ee7181
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Hi,<div><br></div><div>I am learning twisted-web by attemp=
ting each of the examples in the documentation and making any (small) chang=
es necessary for Python 3.=C2=A0 However when I got to the Aysnchronous Res=
ponses example I was not able to get it to work. The example is at:</div><d=
iv><br></div><div><a href=3D"https://twistedmatrix.com/documents/current/we=
b/howto/web-in-60/asynchronous.html">https://twistedmatrix.com/documents/cu=
rrent/web/howto/web-in-60/asynchronous.html</a></div><div><br></div><div>I =
have adapted it to be a self-contained &quot;server&quot; rather than a .rp=
y snippet but=C2=A0<span style=3D"font-size:small;background-color:rgb(255,=
255,255);text-decoration-style:initial;text-decoration-color:initial;float:=
none;display:inline">the response was always a 404.</span> =C2=A0After much=
 head scratching and comparison to other examples, I discovered that by add=
ing &quot;import time&quot; to my &quot;server&quot; it started working.=C2=
=A0</div><div><br></div><div>Why is &quot;import time&quot; required. What =
is happening without the &quot;import time&quot;?</div><div><br></div><div>=
-Jason<br></div><div><div style=3D"font-size:small;background-color:rgb(255=
,255,255);text-decoration-style:initial;text-decoration-color:initial"><br>=
</div><span style=3D"font-size:small;background-color:rgb(255,255,255);text=
-decoration-style:initial;text-decoration-color:initial;float:none;display:=
inline">Cell: 604 644 8611</span><br style=3D"font-size:small;background-co=
lor:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:in=
itial"><span style=3D"font-size:small;background-color:rgb(255,255,255);tex=
t-decoration-style:initial;text-decoration-color:initial;float:none;display=
:inline">Email:<span>=C2=A0</span></span><a href=3D"mailto:drjasonharrison@=
gmail.com" target=3D"_blank" style=3D"color:rgb(17,85,204);font-size:small;=
background-color:rgb(255,255,255)">[email protected]</a><br style=
=3D"font-size:small;background-color:rgb(255,255,255);text-decoration-style=
:initial;text-decoration-color:initial"><span style=3D"font-size:small;back=
ground-color:rgb(255,255,255);text-decoration-style:initial;text-decoration=
-color:initial;float:none;display:inline">LinkedIn:<span>=C2=A0</span></spa=
n><a href=3D"http://www.linkedin.com/in/drjasonharrison" target=3D"_blank" =
style=3D"color:rgb(17,85,204);font-size:small;background-color:rgb(255,255,=
255)">http://www.linkedin.com/in/drjasonharrison</a><br style=3D"font-size:=
small;background-color:rgb(255,255,255);text-decoration-style:initial;text-=
decoration-color:initial"><span style=3D"font-size:small;background-color:r=
gb(255,255,255);text-decoration-style:initial;text-decoration-color:initial=
;float:none;display:inline">Twitter:<span>=C2=A0</span></span><a href=3D"ht=
tp://twitter.com/drjasonharrison" target=3D"_blank" style=3D"color:rgb(17,8=
5,204);font-size:small;background-color:rgb(255,255,255)">http://twitter.co=
m/drjasonharrison</a><br></div><div><br></div><div><br></div><div><br></div=
><div><div><div>#!/usr/bin/env python3</div><div># <a href=3D"https://twist=
edmatrix.com/documents/current/web/howto/web-in-60/asynchronous.html">https=
://twistedmatrix.com/documents/current/web/howto/web-in-60/asynchronous.htm=
l</a></div><div># Asynchronous responses,</div><div>#=C2=A0</div><div># try=
</div><div># - <a href=3D"http://localhost:8888">http://localhost:8888</a><=
/div><div><br></div><div>import sys</div><div>from twisted.web.server impor=
t Site</div><div>from twisted.web.resource import Resource</div><div>from t=
wisted.internet import reactor, endpoints</div><div>from twisted.web.resour=
ce import NoResource</div><div>from twisted.web.server import NOT_DONE_YET<=
/div><div>from twisted.python import log</div><div>import time #???</div><d=
iv><br></div><div>log.startLogging(sys.stdout)</div><div>observer =3D log.P=
ythonLoggingObserver()</div><div>observer.start()</div><div><br></div><div>=
class DelayedResource(Resource):</div><div>=C2=A0 =C2=A0 def _delayedRender=
(self, request):</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 log.msg(&quot;_delay=
edRender request {request!r}&quot;, request =3D request)</div><div>=C2=A0 =
=C2=A0 =C2=A0 =C2=A0 request.write(b&quot;&lt;html&gt;&lt;body&gt;Sorry to =
keep you waiting.&lt;/body&gt;&lt;/html&gt;&quot;)</div><div>=C2=A0 =C2=A0 =
=C2=A0 =C2=A0 request.finish()</div><div><br></div><div>=C2=A0 =C2=A0 def r=
ender_GET(self, request):</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 log.msg(&qu=
ot;get request {request!r}&quot;, request =3D request)</div><div>=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 reactor.callLater(5, self._delayedRender, request)</div><=
div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 return NOT_DONE_YET</div><div><br></div><di=
v>resource =3D DelayedResource()</div><div>factory =3D Site(resource)</div>=
<div>endpoint =3D endpoints.TCP4ServerEndpoint(reactor, 8888)</div><div>end=
point.listen(factory)</div><div>log.msg(&quot;running reactor&quot;)</div><=
div>reactor.run()</div></div></div><div><br></div><div><br></div></div>

--0000000000008987ae0572ee7181--

--===============1203792383131536811==
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Disposition: inline

X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KVHdpc3RlZC13
ZWIgbWFpbGluZyBsaXN0ClR3aXN0ZWQtd2ViQHR3aXN0ZWRtYXRyaXguY29tCmh0dHBzOi8vdHdp
c3RlZG1hdHJpeC5jb20vY2dpLWJpbi9tYWlsbWFuL2xpc3RpbmZvL3R3aXN0ZWQtd2ViCg==

--===============1203792383131536811==--