[Twisted-Python] How Twisted is This?
Christopher Armstrong
radix at twistedmatrix.com
Wed Jul 16 10:38:51 MDT 2003
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
appropriate.
> 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)::
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
WebChecker().doIt('http://twistedmatrix.com/'
).addCallback(lambda *a: reactor.stop())
reactor.run()
--
Twisted | Christopher Armstrong: International Man of Twistery
Radix | Release Manager, Twisted Project
---------+ http://twistedmatrix.com/users/radix.twistd/
More information about the Twisted-Python
mailing list