[Twisted-Python] How Twisted is This?
brad at bbnet.ca
Wed Jul 16 13:24:40 EDT 2003
On Wed, Jul 16, 2003 at 12:38:51PM -0400, Christopher Armstrong wrote:
> On Wed, Jul 16, 2003 at 12:04:30PM -0400, Brad Bollenbach wrote:
> > On Wed, Jul 16, 2003 at 05:45:04PM +0200, Ivo van der Wijk wrote:
> > > Somewhat. You need to make very sure your system doesn't block
> > > unexpectedly :)
> > I'm not sure how you (or Twisted) define "unexpected", but I probably
> > want things to block when -- for example -- I try to retrieve a page
> > from a certain URL.
> "unexpectedly" means "at all" :-)
> Are you sure you _really_ mean this? Is there some user interface bit
> that you're implying when you say you want it to block? Do you just
> mean that you want to wait for some tasks to complete before you start
> others? If so, then that's not blocking, and Twisted is still
> > I certainly don't want to be writing state machines.
> That's how Twisted works. But it's not annoying if you have a good
> standard library of state machines already :-) For example,
> twisted.web.client and/or twisted.web.monitor already exist, so all
> you would have to do to implement the web checker (using t.w.client)
> is this (I'm assuming you know the basics of Deferreds)::
Well, I'm sure I mean I want to block.
Unless I don't (in which case Twisted would have to prove to me that I
Here's an example use case for a monitoring check:
1. open web page and store cookie that it gives us
2. did we get the page ok? (yes)
3. submit the form with values foo=bar and baz=spam (and the cookie we
got in step 1)
4. did the response get retrieved ok? (yes)
5. is the string "Welcome to blah blah blah" present in this page? (no)
6. return a "NOT OK" for this monitor
Using a state machine for this seems like too much complexity, compared
to just starting a thread and letting me simply block so I can do these
checks procedurally (albeit inside a method, if you know what I mean).
> from twisted.web import client
> class WebChecker:
> def doIt(self, url):
> return client.getPage(url,
> ).addCallback(self.gotPage, url
> ).addErrback(self.reportError, url)
> def gotPage(self, page, url):
> print "hooray, got page %s" % url
> #self.checkForFoo(page, url), etc
> def reportError(self, failure, url):
> print ":( There was a problem in %s" % url
> print failure
> To try out this code, do
> from twisted.internet import reactor
> ).addCallback(lambda *a: reactor.stop())
Thanks for this example.
I'll mull over this and see if I can mold it into the different checks I
need to perform, and whether or not state-machinery will make life
easier or more complex.
More information about the Twisted-Python