Discussion:
[Twisted-Python] Asynchronously initializing objects in twisted.web?
Nagy, Attila
2015-09-08 14:51:04 UTC
Permalink
Hi,

I wonder, if I have this example program:
https://twistedmatrix.com/documents/current/web/howto/using-twistedweb.html#site-objects

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?
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).

Thanks,
Phil Mayers
2015-09-08 16:07:15 UTC
Permalink
Post by Nagy, Attila
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
Load the resources in a callback using "reactor.callWhenRunning" and
only then call reactor.listenTCP or whatever.
Glyph
2015-09-08 22:21:01 UTC
Permalink
Load the resources in a callback using "reactor.callWhenRunning" and only then call reactor.listenTCP or whatever.
Just trying to use every opportunity I can to say this: don't use listenTCP, use endpoints: https://twistedmatrix.com/documents/15.4.0/core/howto/endpoints.html <https://twistedmatrix.com/documents/15.4.0/core/howto/endpoints.html>

:)

-glyph
Alexey Panyovin
2015-09-08 18:41:06 UTC
Permalink
from twisted.webimport server, resource
from twisted.internetimport reactor, defer

class Simple(resource.Resource):
isLeaf =True def render_GET(self, request):
return "<html>Hello, world!</html>" def call_me():
d = defer.Deferred()
print "Get something" d.callback("success")
return d

def run_web(_):
print "run_web" site = server.Site(Simple())
reactor.listenTCP(8080, site)

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

--
Alex
Post by Nagy, Attila
Hi,
https://twistedmatrix.com/documents/current/web/howto/using-twistedweb.html#site-objects
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?
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).
Thanks,
_______________________________________________
Twisted-Python mailing list
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
Nagy, Attila
2015-09-09 07:27:46 UTC
Permalink
Ah, I was under the impression that listenTCP() can only be called
before reactor.run().
I don't now where it came from.

Thanks,
Post by Alexey Panyovin
from twisted.webimport server, resource
from twisted.internetimport reactor, defer
d = defer.Deferred()
print "Get something" d.callback("success")
return d
print "run_web" site = server.Site(Simple())
reactor.listenTCP(8080, site)
d = call_me()
d.addCallback(run_web)
reactor.run()
--
Alex
Post by Nagy, Attila
Hi,
https://twistedmatrix.com/documents/current/web/howto/using-twistedweb.html#site-objects
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?
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).
Thanks,
_______________________________________________
Twisted-Python mailing list
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
_______________________________________________
Twisted-Python mailing list
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
Loading...