Re: Asynchronous responses example from documentation -- why import time??
Jean-Paul Calderone <[email protected]> Wed, 8 Aug 2018 11:58:37 -0400
| Newsgroups | gmane.comp.python.twisted.web |
|---|---|
| Message-ID | <CAEeXt4P3z1qBY8dD6GrGr2TUX4LHMSFByzFN07NQi11vW81ivw@mail.gmail.com> |
--===============0096989382780468247== Content-Type: multipart/alternative; boundary="0000000000001641dd0572ee9657" --0000000000001641dd0572ee9657 Content-Type: text/plain; charset="UTF-8" On Wed, Aug 8, 2018 at 11:51 AM Jason Harrison <[email protected]> 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.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"? > > 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: [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() > > > _______________________________________________ > Twisted-web mailing list > [email protected] > https://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web > --0000000000001641dd0572ee9657 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr"><div class=3D"gmail_quote"><div dir=3D"ltr">On Wed, Aug 8,= 2018 at 11:51 AM Jason Harrison <<a href=3D"mailto:drjasonharrison@gmai= l.com">[email protected]</a>> wrote:<br></div><blockquote class= =3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padd= ing-left:1ex"><div dir=3D"ltr">Hi,<div><br></div><div>I am learning twisted= -web by attempting each of the examples in the documentation and making any= (small) changes necessary for Python 3.=C2=A0 However when I got to the Ay= snchronous Responses example I was not able to get it to work. The example = is at:</div><div><br></div><div><a href=3D"https://twistedmatrix.com/docume= nts/current/web/howto/web-in-60/asynchronous.html" target=3D"_blank">https:= //twistedmatrix.com/documents/current/web/howto/web-in-60/asynchronous.html= </a></div><div><br></div><div>I have adapted it to be a self-contained &quo= t;server" rather than a .rpy 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 alway= s a 404.</span> =C2=A0After much head scratching and comparison to other ex= amples, I discovered that by adding "import time" to my "ser= ver" it started working.=C2=A0</div><div><br></div><div>Why is "i= mport time" required. What is happening without the "import time&= quot;?</div><div><br></div></div></blockquote><div><br></div><div>One possi= ble explanation is that it has nothing to do with <font face=3D"monospace, = monospace">import time</font><font face=3D"arial, helvetica, sans-serif">= =C2=A0per se.=C2=A0 Instead, <i>any</i>=C2=A0change to your source file wou= ld have fixed the problem.=C2=A0 This could be because you had "stale&= quot; pyc files lying around (cached bytecode the interpreter <i>thought</i= >=C2=A0was up-to-date with your source but was actually outdated).=C2=A0 It= could also be that your server process was left running and was still usin= g your old code.=C2=A0 Then, for some reason, after you added the </font><f= ont face=3D"monospace, monospace">import time</font><font face=3D"arial, he= lvetica, sans-serif">=C2=A0your server got the new version of the code (per= haps you're relying on an auto-reloader and it missed a change, for exa= mple, or you just forgot to restart the server yourself).</font></div><div>= <font face=3D"arial, helvetica, sans-serif"><br></font></div><div><font fac= e=3D"monospace, monospace">import time</font><font face=3D"arial, helvetica= , sans-serif">=C2=A0itself is definitely not a requirement for arbitrary Tw= isted Web-based programs to return a response other than 404.</font></div><= div><font face=3D"arial, helvetica, sans-serif"><br></font></div><div><font= face=3D"arial, helvetica, sans-serif">Jean-Paul</font></div><div>=C2=A0</d= iv><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left= :1px #ccc solid;padding-left:1ex"><div dir=3D"ltr"><div></div><div>-Jason<b= r></div><div><div style=3D"font-size:small;background-color:rgb(255,255,255= );text-decoration-style:initial;text-decoration-color:initial"><br></div><s= pan style=3D"font-size:small;background-color:rgb(255,255,255);text-decorat= ion-style:initial;text-decoration-color:initial;float:none;display:inline">= Cell: 604 644 8611</span><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:rgb(255,255,255);text-decora= tion-style:initial;text-decoration-color:initial;float:none;display:inline"= >Email:<span>=C2=A0</span></span><a href=3D"mailto:[email protected]= m" style=3D"color:rgb(17,85,204);font-size:small;background-color:rgb(255,2= 55,255)" target=3D"_blank">[email protected]</a><br style=3D"font-s= ize:small;background-color:rgb(255,255,255);text-decoration-style:initial;t= ext-decoration-color:initial"><span style=3D"font-size:small;background-col= or:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:ini= tial;float:none;display:inline">LinkedIn:<span>=C2=A0</span></span><a href= =3D"http://www.linkedin.com/in/drjasonharrison" style=3D"color:rgb(17,85,20= 4);font-size:small;background-color:rgb(255,255,255)" target=3D"_blank">htt= p://www.linkedin.com/in/drjasonharrison</a><br style=3D"font-size:small;bac= kground-color:rgb(255,255,255);text-decoration-style:initial;text-decoratio= n-color:initial"><span style=3D"font-size:small;background-color:rgb(255,25= 5,255);text-decoration-style:initial;text-decoration-color:initial;float:no= ne;display:inline">Twitter:<span>=C2=A0</span></span><a href=3D"http://twit= ter.com/drjasonharrison" style=3D"color:rgb(17,85,204);font-size:small;back= ground-color:rgb(255,255,255)" target=3D"_blank">http://twitter.com/drjason= harrison</a><br></div><div><br></div><div><br></div><div><br></div><div><di= v><div>#!/usr/bin/env python3</div><div># <a href=3D"https://twistedmatrix.= com/documents/current/web/howto/web-in-60/asynchronous.html" target=3D"_bla= nk">https://twistedmatrix.com/documents/current/web/howto/web-in-60/asynchr= onous.html</a></div><div># Asynchronous responses,</div><div>#=C2=A0</div><= div># try</div><div># - <a href=3D"http://localhost:8888" target=3D"_blank"= >http://localhost:8888</a></div><div><br></div><div>import sys</div><div>fr= om twisted.web.server import Site</div><div>from twisted.web.resource impor= t Resource</div><div>from twisted.internet import reactor, endpoints</div><= div>from twisted.web.resource import NoResource</div><div>from twisted.web.= server import NOT_DONE_YET</div><div>from twisted.python import log</div><d= iv>import time #???</div><div><br></div><div>log.startLogging(sys.stdout)</= div><div>observer =3D log.PythonLoggingObserver()</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("_delayedRender request {request!r}", request = =3D request)</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 request.write(b"<= ;html><body>Sorry to keep you waiting.</body></html>&q= uot;)</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 request.finish()</div><div><br>= </div><div>=C2=A0 =C2=A0 def render_GET(self, request):</div><div>=C2=A0 = =C2=A0 =C2=A0 =C2=A0 log.msg("get request {request!r}", request = =3D request)</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 reactor.callLater(5, sel= f._delayedRender, request)</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 return NOT= _DONE_YET</div><div><br></div><div>resource =3D DelayedResource()</div><div= >factory =3D Site(resource)</div><div>endpoint =3D endpoints.TCP4ServerEndp= oint(reactor, 8888)</div><div>endpoint.listen(factory)</div><div>log.msg(&q= uot;running reactor")</div><div>reactor.run()</div></div></div><div><b= r></div><div><br></div></div> _______________________________________________<br> Twisted-web mailing list<br> <a href=3D"mailto:[email protected]" target=3D"_blank">Twisted-= [email protected]</a><br> <a href=3D"https://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web" = rel=3D"noreferrer" target=3D"_blank">https://twistedmatrix.com/cgi-bin/mail= man/listinfo/twisted-web</a><br> </blockquote></div></div> --0000000000001641dd0572ee9657-- --===============0096989382780468247== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: inline X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KVHdpc3RlZC13 ZWIgbWFpbGluZyBsaXN0ClR3aXN0ZWQtd2ViQHR3aXN0ZWRtYXRyaXguY29tCmh0dHBzOi8vdHdp c3RlZG1hdHJpeC5jb20vY2dpLWJpbi9tYWlsbWFuL2xpc3RpbmZvL3R3aXN0ZWQtd2ViCg== --===============0096989382780468247==--