Beispiel #1
0
    def get(self, path):
        s = time.time()
        try:
            cached = self.CACHE.get(path)
            self.deliver(cached)
            self.flush()
            return
        except KeyError:
            pass

        url = self.get_worker_url(path)
        client = thc.AsyncHTTPClient()
        response = yield gen.Task(client.fetch, url)
        if response.error:
            self.set_status(response.code)
            self.write(str(response.error))
            self.finish()
            return
        else:
            # cache the response
            image = cached_image(body=response.body,
                                 content_type=response.headers.get('Content-Type'),
                                 size=len(response.body))
            self.CACHE.put(path, image)
            self.deliver(image)
            self.finish()
            return 
Beispiel #2
0
    def on_fetch(self, response):
        im = None
        if response.error:
            logging.error('%s got a %s %s', 
                          response.request.url, 
                          response.code,
                          response.error)
        else:
            ctype = response.headers.get('Content-Type')
            cimg = cached_image(body=response.body,
                                content_type=ctype,
                                size=len(response.body))

            if self._cache:
                self._cache.put(path, cimg)
            
            im = Image.open(StringIO(cimg.body))

        waiters = self._inflight.pop(response.request.url)
        logging.debug('popping %s waiters for %s', len(waiters),
                      response.request.url)
        for c in waiters:
            c(im)