Richard Bucker

TornadoWeb - web scraping, eventd, recursion

Posted at — Nov 1, 2011

I’m working with TornadoWeb and ZeroMQ at the moment and I was having a heck of a time getting things to work correctly. Especially when I was trying to call-out to another webserver and preserve the async-mode in the system. (TornadoWeb provides a basic Asyncronous HTTP Client library)First of all, some months ago I found a post that wanted me to replace Tornado’s IOLoop with the ZMQ version. They did not state why but they were specific to say that Tornado’s was to be replaced with ZMQ and not the other way around. The code was not very interesting:# override tornado’s ioloop with zmq’sioloop.IOLoop = zmq.eventloop.ioloop.IOLoopAnd while I was researching this project I found that ZMQ provides a method that does the work for you. I reviewed the code and there was nothing special in it. It was pretty much the same code as I had originally implemented. I changed the code to use the new method. Not because they did it better but because I am hoping that this might future proof my project incase there is a change that I cannot account for. So the code now looks like:# override tornado’s ioloop with zmq’s#ioloop.IOLoop = zmq.eventloop.ioloop.IOLoopzmq.eventloop.ioloop.install()Hey! Nothing spectacular there. Moving on to the next challenge…The code I’m currently working on is an admin site. Admin in the sense that only one or two users will actually every use this app and it would be very unlikely that more that one user would be online (even in an emergency). While that is true… the synchronous version of the website did not perform very well. Specially when I was deleting large amounts of data from Redis.In this particular case I’m trying to implement a “test harness”. While I like nosetests and it works great I need something that is interactive and ubiquitous. In this use-case the user enters the URL for the test-index-page. The page is drawn from a dictionary of testcases. The user clicks on a testcase and it runs to completion… drawing it’s output in the buffer and then going back (recursively) to the admin website and putting in some additional data (a call trace) and appending that to the buffer too. The challenge was several fold. a) the build-in classes were not working asynchronously, b) there were two client calls to make; 1) the authorization 2) the call trace. c) make it all work within the asynchronous framework.Before I go any further. It works and here is the code. I’m not going to explain it any more than this for now.# test handlerclass TestHandler(tornado.web.RequestHandler): “" “" def initialize(self): self.server = ‘http://myapiserver.local:8882’ self.adminserver = ‘http://myadminswerver:8881’ self.path = ‘api1.1’ super(TestHandler,self).initialize() # normally the callback function does not get this decorator, however, this # was needed in order to make this work. Notice that this is the second handler @asynchronous def _handle_request2(self, response): ““This is the second callback handler. “" if response.error: self.write(“Error: %s” % (response.error)) else: self.write(response.body) # need the self.finish() because the asynchronous decorator # disables the auto_finish() self.finish() # since this is the first @asynchronous def _handle_request1(self, response): ““This is the first callback handler. “" if response.error: self.write(“Error: %s” % (response.error)) else: # write the output to the buffer but since we are not calling # finish() the data remains in the buffer. self.write(response.body) # make the second call and callback to the second handler. url = “%s/myfunction_two/” % (self.adminserver) request = httpclient.HTTPRequest(url) # it is important to replace the io_loop here (also needed to make it work) http_client = httpclient.AsyncHTTPClient(io_loop=ioloop.IOLoop.instance()) # going to callback to the 2nd handler http_client.fetch(request, self._handle_request2) @asynchronous def _get(self,other=None): url = “%s/%s” % (self.server, path or self.path) pay = self.path request = httpclient.HTTPRequest(url, body=pay, method=‘POST’) # it is important to replace the io_loop here (also needed to make it work) http_client = httpclient.AsyncHTTPClient(io_loop=ioloop.IOLoop.instance()) # going to callback to the 1st handler http_client.fetch(request, self._handle_request1) # notice that there is NO decorator here. It will be applied when _get() is called. def get(self,other=None): ““display the menu or execute the test “" self.guid = str(uuid.uuid1()) if not other: # display the testcase menu for k in testcases.keys(): self.write('<a href="/t/%s/">%s</a><br>' % (k, k)) self.finish() else: self._get(other)It would have been nice if there had been a “parallel” task execution as the two queries could be executed at the same time because they are unrelated requests. Granted the callback would have to juggle the results in order to get them in the right order and then display them I suppose it might be possible with a single handler if the handler could inspect the data before calling finish(). It’s something worth posting in the future.I also want to mention that a similar strategy would probably apply to Mojolicious. (assignment for the reader; I home someone will post and link back.)