[Twisted-Python] Deferred model
Hello Can someone provide me a good example of a deferred model in woven ? I have a big model to build, and the page takes a long time to render... I didn't find any help on twisted site :( thx for help -- Philippe
Philippe Lafoucrière <lafou@wanadoo.fr> writes:
Hello
Can someone provide me a good example of a deferred model in woven ? I have a big model to build, and the page takes a long time to render...
from twisted.web.woven.page import Page from twised.internet import defer class OurPage(Page): template = '''<html><body> <p model="deferredModel" view="Text"></p> </body></html>''' def wmfactory_deferredModel(self, request): """ returns an already callbacked Deferred with the string 'Hello world' as it's result """ return defer.succeed('Hello world') Okay? -- Syver Enstad
from twisted.web.woven.page import Page from twised.internet import defer
class OurPage(Page): template = '''<html><body> <p model="deferredModel" view="Text"></p> </body></html>''' def wmfactory_deferredModel(self, request): """ returns an already callbacked Deferred with the string 'Hello world' as it's result """ return defer.succeed('Hello world')
Okay?
Lovely :) thank you !
from twisted.web.woven.page import Page from twised.internet import defer
class OurPage(Page): template = '''<html><body> <p model="deferredModel" view="Text"></p> </body></html>''' def wmfactory_deferredModel(self, request): """ returns an already callbacked Deferred with the string 'Hello world' as it's result """ return defer.succeed('Hello world')
Last question (sry, I'm still a newbie in twisted :p) : where do I put my blocking code ? just before the defer.succeed ??
On Thu, 2003-11-13 at 11:39, Philippe Lafoucrière wrote:
where do I put my blocking code ? just before the defer.succeed ??
I'm sure this has been answered a million times before, but here goes... Deferreds do not magically turn blocking code into non-blocking code. If you place blocking code in a wmfactory_ method, it will still interfere with the event loop. If you have blocking code, you can either rewrite it in a non-blocking manner, or you can use a separate thread to run it. <http://twistedmatrix.com/documents/howto/threading> If you give more detail about what you're trying to do, I'm sure one of the Twisted gurus can explain what the best approach(es) might be. -- Alex Levy WWW: http://mesozoic.geecs.org "Never let your sense of morals prevent you from doing what is right." -- Salvor Hardin, Isaac Asimov's _Foundation_
I'm sure this has been answered a million times before, but here goes...
I'm sure too. But I proposed a long time ago (and several times) to use a forum instead of a mailing list...
Deferreds do not magically turn blocking code into non-blocking code. If you place blocking code in a wmfactory_ method, it will still interfere with the event loop.
If you have blocking code, you can either rewrite it in a non-blocking manner, or you can use a separate thread to run it. <http://twistedmatrix.com/documents/howto/threading>
If you give more detail about what you're trying to do, I'm sure one of the Twisted gurus can explain what the best approach(es) might be.
I thought of using the setUp method of page. I can start the calculation when the page loads, but I don't know what to call (in the callback) when it's done (a wmfactory_xxx ?). I'll check the threading part you gave me. Thank you ! -- Philippe
On Thu, Nov 13, 2003 at 10:14:16PM +0100, Philippe Lafoucri?re wrote:
I'm sure this has been answered a million times before, but here goes...
I'm sure too. But I proposed a long time ago (and several times) to use a forum instead of a mailing list...
This is a terrible excuse. Not only do we have our *own* archives: http://twistedmatrix.com/pipermail/twisted-python which, of course, are searchable via google: "deferred blocking code site:twistedmatrix.com" there are *multiple* archives elsewhere that have a built-in search functionality as well: http://marc.free.net.ph/list/twisted-python.html http://news.gmane.org/gmane.comp.python.twisted I found these with a trivial amount of googling: "twisted-python mailing list" Having searchable archives is obviously no reason to switch to a web forum (ugh), as we have that right now.
If you give more detail about what you're trying to do, I'm sure one of the Twisted gurus can explain what the best approach(es) might be.
I thought of using the setUp method of page. I can start the calculation when the page loads, but I don't know what to call (in the callback) when it's done (a wmfactory_xxx ?). I'll check the threading part you gave me. Thank you !
Unfortunately, I don't think there's any special support in Woven to handle Deferreds from setUp. You can return Deferreds from wmfactory_XXX and Woven will handle them correctly: it will not finish rendering the page until that Deferred has fired and the model is ready. -- Twisted | Christopher Armstrong: International Man of Twistery Radix | Release Manager, Twisted Project ---------+ http://radix.twistedmatrix.com/
On Thu, Nov 13, 2003 at 05:04:15PM -0500, Christopher Armstrong wrote:
On Thu, Nov 13, 2003 at 10:14:16PM +0100, Philippe Lafoucri?re wrote:
I'm sure this has been answered a million times before, but here goes...
I'm sure too. But I proposed a long time ago (and several times) to use a forum instead of a mailing list...
This is a terrible excuse. Not only do we have our *own* archives: [...]
And, of course, the mailing list isn't the only source of documentation. The official docs are far from perfect, but they're getting better (over 300 pages as a PDF, when I last checked!). For cases like this question, there's the FAQ. I've just checked in an addition to it that hopefully addresses this question. It should be visible here in a few minutes, once buildbot does its thing: http://twistedmatrix.com/users/warner/doc-latest/howto/faq.html#deferreds-aren't-magic -Andrew.
http://twistedmatrix.com/users/warner/doc-latest/howto/faq.html#deferreds-aren't-magic
the link http://twistedmatrix.com/documents/TwistedDocs/TwistedDocs-/api/twisted.inte... is broken in the faq (defertoThread)
On Fri, Nov 14, 2003 at 02:01:12PM +0100, Philippe Lafoucrière wrote:
http://twistedmatrix.com/users/warner/doc-latest/howto/faq.html#deferreds-aren't-magic
the link http://twistedmatrix.com/documents/TwistedDocs/TwistedDocs-/api/twisted.inte... is broken in the faq (defertoThread)
Yeah, that's a known glitch; the docs in the doc-latest directory aren't generated with valid API links. I've just checked in a change that will make the doc-latest docs generate links to http://twistedmatrix.com/documents/TwistedDocs/current/... which will fix the worst of the broken links (there are likely to still be some, seeing as that is linking CVS howtos to the release version of the API docs). There's a seperate bug with the way links to top-level functions in modules work (as opposed to classes and methods of classes) which means that particular link will still not work for now. It'll work when we do another release, though, which is what matters. doc-latest docs are just a preview of the next release, after all, not the final product :) Anyway, aside from that link, I take it you are happy with that FAQ entry? :) -Andrew.
If you give more detail about what you're trying to do, I'm sure one of the Twisted gurus can explain what the best approach(es) might be.
I'm trying to manage my gentoo box with a twisted server (A linux Distro). Portage is very slow, and build the package first level tree can take several seconds. I just wanted my page not to block while loading, and put the result in it when it becomes available. The list of the category is in a "wmupdate_category(self, request)" method, in which I'd like to use deferreds. Thank you gurus for your help ! -- Philippe - kind of lost sometimes with twisted :p
participants (5)
-
Alex Levy
-
Andrew Bennetts
-
Christopher Armstrong
-
Philippe Lafoucrière
-
Syver Enstad