<html>
  <head>
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <pre style="background-color:#ffffff;color:#000000;font-family:'Menlo';font-size:12,0pt;"><span style="color:#000080;font-weight:bold;">from </span>twisted.web <span style="color:#000080;font-weight:bold;">import </span>server<span style="color:#cc7832;">, </span>resource
<span style="color:#000080;font-weight:bold;">from </span>twisted.internet <span style="color:#000080;font-weight:bold;">import </span>reactor<span style="color:#cc7832;">, </span>defer

<span style="color:#000080;font-weight:bold;">class </span>Simple(resource.Resource):
    isLeaf = <span style="color:#000080;">True
</span><span style="color:#000080;">    </span><span style="color:#000080;font-weight:bold;">def </span>render_GET(<span style="color:#94558d;">self</span><span style="color:#cc7832;">, </span><span style="color:#808080;">request</span>):
        <span style="color:#000080;font-weight:bold;">return </span><span style="color:#008000;font-weight:bold;">"<html>Hello, world!</html>"
</span><span style="color:#008000;font-weight:bold;">
</span><span style="color:#000080;font-weight:bold;">def </span>call_me():
    d = defer.Deferred()
    <span style="color:#000080;font-weight:bold;">print </span><span style="color:#008000;font-weight:bold;">"Get something"
</span><span style="color:#008000;font-weight:bold;">    </span>d.callback(<span style="color:#008000;font-weight:bold;">"success"</span>)
    <span style="color:#000080;font-weight:bold;">return </span>d

<span style="color:#000080;font-weight:bold;">def </span>run_web(_):
    <span style="color:#000080;font-weight:bold;">print </span><span style="color:#008000;font-weight:bold;">"run_web"
</span><span style="color:#008000;font-weight:bold;">    </span>site = server.Site(Simple())
    reactor.listenTCP(<span style="color:#0000ff;">8080</span><span style="color:#cc7832;">, </span>site)

d = call_me()
d.addCallback(run_web)
reactor.run()

</pre>
    <pre class="moz-signature" cols="72">--
Alex

08.09.15 17:51, Nagy, Attila пишет:
</pre>
    <blockquote cite="mid:55EEF5D8.7020709@fsn.hu" type="cite">Hi,
      <br>
      <br>
      I wonder, if I have this example program:
      <br>
<a class="moz-txt-link-freetext" href="https://twistedmatrix.com/documents/current/web/howto/using-twistedweb.html#site-objects">https://twistedmatrix.com/documents/current/web/howto/using-twistedweb.html#site-objects</a>
      <br>
      <br>
      how do I initialize an external resource (like a dictionary, which
      is fetched from a database) with asynchronous twisted tools, but
      before the HTTP server starts listening on its port?
      <br>
      The goal here is that the HTTP server shouldn't start until it can
      fetch the desired data. Of course I can do this in a synchronous
      way, but it feels so unnatural and bad practice to fetch the same
      stuff synchronously on initialization and asynchronously when the
      program/reactor is running (often with different libraries).
      <br>
      <br>
      Thanks,
      <br>
      <br>
      _______________________________________________
      <br>
      Twisted-Python mailing list
      <br>
      <a class="moz-txt-link-abbreviated" href="mailto:Twisted-Python@twistedmatrix.com">Twisted-Python@twistedmatrix.com</a>
      <br>
      <a class="moz-txt-link-freetext" href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python</a>
      <br>
    </blockquote>
    <br>
  </body>
</html>