Lucas
Lucas

Reputation: 1279

Google App Engine Python: Image retrieved from a database query loading slowly in production

I have an app where the user can upload an image from disk. That image will be shown in every page of the application as long as the user is logged.

This is the portion of the template that loads an image to the page:

{% if currentService.image %}
    <img src='img?img_id={{ currentService.key() }}'></img>
{% else %}
    <img src='static/no_image.jpeg' />
{% endif %}

This is how I make the redirect in the controller:

def main():
    app = webapp2.WSGIApplication([
                                   ('/img', ImageHandler),
                                   ('/.*', MainPage)
                                   ],debug=True)
    wsgiref.handlers.CGIHandler().run(app)

and this is the corresponding handler class:

class ImageHandler(webapp2.RequestHandler):
    def get(self):
        try:
            rsRequest = db.get(self.request.get("img_id"))
            if rsRequest.image:
                self.response.headers['Content-Type'] = "image/png"
                self.response.out.write(str(rsRequest.image))
            else:
                self.response.out.write("No Image")
        except:
            tb = traceback.format_exc()
            ErrorLog(descripcion = str(tb)).put()
            self.doRender('error_page.html', {'mensaje':tb})

This is the model class:

class Service(db.Model):
    name = db.StringProperty(required = True)
    image = db.BlobProperty()

Everything works fine. The image is always loaded successfully.

But when I test my app in production the image loads very slow, progressively.

I don't know what I'm doing wrong since the image loads fast (like a cached image) in development when I test the app in localhost.

What should I do to avoid that slow image load?

Upvotes: 0

Views: 224

Answers (2)

Haldean Brown
Haldean Brown

Reputation: 12711

Try serving your images from Blobstore, which is designed to serve big static payloads quickly. It's probably fast on your local host because dev_appserver keeps the datastore in memory, but in production it's much slower.

Upvotes: 2

voscausa
voscausa

Reputation: 11706

There can be a lot of reasons. Did you use the chrome developer tools?

But there is a smart solution. Google can serve the images for you. Almost for free and very fast. Look at get_serving_url of the GAE Python image API. Here is an example (or use it without https):

https://lh6.ggpht.com/lOghqU2JrYk8M-Aoio8WjMM6mstgZcTP0VzJk79HteVLhnwZy0kqbgVGQZYP8YsoqVNzsu0EBysX16qMJe7H2BsOAr4j=s70

Upvotes: 1

Related Questions