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?
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.