[Twisted-Python] Simplifying my StatusMonitor

Wari Wahab wari at home.wari.org
Thu Jul 17 23:42:04 EDT 2003

Brad Bollenbach wrote:

>Okay, in the usual XP fashion, I'm doing a spike to assess two use cases
>for Twisted:
>1. Writing a simple monitor that checks the status of a response, to see
>what it's like to write a foobar in Twisted.
>I did #1 this morning. It's 49 lines of Python, in what would have
>otherwise been less than five of "normal", blocking code:
>[..snip code..]]
>How could I have made this simpler? What approach would you have taken
>to solve this problem?
Although the code seems like a lot, but it looks like you've gained 
something that does not block. For example, you can check a few sites 
concurrently this way.

I changed the code a bit so that I get more interesting results out of 
it. And from the results, you see that the return of the status != the 
order you called it, which is the nature of the internet.
wari at minisync:~$ python testspike.py
Got request for http://www.bbnet.ca/
Got request for http://roughingit.wari.org/
Got request for http://www.yahoo.com/
Got request for http://www.twistedmatrix.com/
hooray, got status 200 for http://roughingit.wari.org/
hooray, got status 200 for http://www.twistedmatrix.com/
hooray, got status 200 for http://www.yahoo.com/
hooray, got status 200 for http://www.bbnet.ca/

The call I did is this:
#d.addCallback(lambda *a: reactor.stop())
reactor.callLater(5, reactor.stop)

PS: To twisted developers, why can't I do
d = StatusMonitor().checkFor200OK('http://www.bbnet.ca/')
d.addCallback(StatusMonitor().checkFor200OK, 'http://www.yahoo.com')

The reactor will be stuck after the bbnet.ca call.

More information about the Twisted-Python mailing list